Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

jQuery not getting reply when using asynchronous

I am using this code to get response back from server. But I want that asynchronously as I need to show progress. When I use this code, I don't get any messsage back from server (only getting alert as object). Here is my code. Any ideas?

var msg = $.ajax({
    type: 'POST',
    url: '../pages/registration.php',
    async: true,
    data: {
        do: 'register'
    },
    beforeSend: function () {
        $("#Progress").show();
    },
    success: function (data, status) {
        $("#Progress").hide();
        $("#AckMsg").html(data);
    }
});
alert("aa" + msg);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of jQuery 1.5, a XMLHttpRequest (jqXHR) object is returned by $.ajax(). This is the object you assign to your var msg, and later put in the alert. Hence you get "alert as object" as you describe it. If you want to alert what is returned by the server, do:

success: function (data, status) {
    $("#Progress").hide();
    $("#AckMsg").html(data);
    alert(data);
}

If this alerts data as empty or undefined. Check your ajax call URL.

url: '../pages/registration.php',

I believe using a relative URL like this will make a ajax request to http://www.yoursite.com/../pages/registration.php for example. This is most likely not what you want to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...