代码不在 $.get() 成功处理程序内运行

发布于 2024-11-09 05:32:33 字数 778 浏览 0 评论 0原文

这是我的两个 jQuery 函数

$(".enable_button").click(function(){
    var server_name = $(this).attr("title");
    $.get("ajax/server_ajax.php?action=enable&server_name=" + server_name), function(data) {
        alert(data);
    }
});

$(".disable_button").click(function(){
    var server_name = $(this).attr("title");
    $.get("ajax/server_ajax.php?action=disable&server_name=" + server_name), function(data) {
        alert(data);
    }
});

很容易看到它们的作用,单击按钮,它们通过将 action 参数和 server_name 参数传递给 来启用或禁用服务器ajax/server_ajax.php。一切正常,但由于某种原因,返回的数据永远不会被警告。我可以验证它是否正确请求 PHP 文件并且 PHP 文件正在返回数据,正如我在 Firebug 的“响应”选项卡中看到的那样。

我还尝试用 document.write(data) 替换警报,但这也不起作用。

有人可以帮忙吗?我一无所知。

These are my two jQuery functions

$(".enable_button").click(function(){
    var server_name = $(this).attr("title");
    $.get("ajax/server_ajax.php?action=enable&server_name=" + server_name), function(data) {
        alert(data);
    }
});

$(".disable_button").click(function(){
    var server_name = $(this).attr("title");
    $.get("ajax/server_ajax.php?action=disable&server_name=" + server_name), function(data) {
        alert(data);
    }
});

Pretty easy to see what they do, on a button click they enable or disable a server by passing an action parameter and server_name parameter to ajax/server_ajax.php. Everthing works, but for some reason the data being returned is never being alert'd. I can verify that it is correctly requesting that PHP file and the PHP file is returning data, as I can see it in the "Response" tab of Firebug.

I've also tried replacing the alert with document.write(data) and that doesn't work either.

Can anyone lend a hand? I'm clueless.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-11-16 05:32:33

您过早地关闭了对 get() 的调用。 + server_name 后面不应该有括号。

$.get("ajax/server_ajax.php?action=disable&server_name=" + server_name), function(data) {
                                                                    //^The ) shouldn't be here
    alert(data);
}

应该

$.get("ajax/server_ajax.php?action=disable&server_name=" + server_name, function(data) {
    alert(data);
}); //<-- close the call to get( over here, not after server_name.

只是旁白,但是使用 jQuery 的 ajax 函数,您可以传递数据对象,并且 jQuery 将从中创建查询字符串,巧妙地转义任何需要它的内容。所以你可以这样做,这相当于你原来的禁用调用:

$.get("ajax/server_ajax.php", {"action" : "disable", "server_name" : server_name}, function(data) {
    alert(data);
});

You're closing the call to get() too early. The parenthesis after + server_name shouldn't be there.

$.get("ajax/server_ajax.php?action=disable&server_name=" + server_name), function(data) {
                                                                    //^The ) shouldn't be here
    alert(data);
}

should be

$.get("ajax/server_ajax.php?action=disable&server_name=" + server_name, function(data) {
    alert(data);
}); //<-- close the call to get( over here, not after server_name.

Just an aside but with jQuery's ajax functions, you have the ability to pass an object for the data and jQuery will create the query string out of it, neatly escaping anything requires it. So you might do this which is equivalent to your original call for disable:

$.get("ajax/server_ajax.php", {"action" : "disable", "server_name" : server_name}, function(data) {
    alert(data);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文