代码不在 $.get() 成功处理程序内运行
这是我的两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您过早地关闭了对
get()
的调用。+ server_name
后面不应该有括号。应该
只是旁白,但是使用 jQuery 的 ajax 函数,您可以传递数据对象,并且 jQuery 将从中创建查询字符串,巧妙地转义任何需要它的内容。所以你可以这样做,这相当于你原来的禁用调用:
You're closing the call to
get()
too early. The parenthesis after+ server_name
shouldn't be there.should be
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: