Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...