开发者

Code isn't being run inside the $.get() success handler

开发者 https://www.devze.com 2023-03-07 23:56 出处:网络
These are my two jQuery functions $(\".enable_button\").click(function(){ var server_name = $(this).attr(\"title\");

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.


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);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消