MAO-EYE 2013-08-09 09:46 采纳率: 66.7%
浏览 19

使用ajax的POST不可靠

I need to send and receive some simple data with my mobile application. I do this using the jquery $.post function - but it seems to be very unstable.

Problem: Often, like 1 out of 10 times, the POST is sent, but the callback function is never run. I know from the serverside that the POST is received and that data is returned - yet often there's nothing happening on the clientside.

$.post(url, { 'username': username, 'password': password },
function(data) {
    if (data.Message == "Ok") {
        alert("Logged in OK.");
    } else if (data.Message == "Error") {
        alert("Error. Check username and password.");
    } else {
        alert("An unknown error has occured.");
    }
});

The times when nothing happens on the client side (even though I see data has been sent to the server) - running the function again doesn't work. I have to reload the entire page and try again - and then everything works as normal.

The setup is jquery together with Kendo UI Mobile.

Is there another, more reliable way, to make this POST request using JavaScript?

  • 写回答

2条回答 默认 最新

  • weixin_33727510 2013-08-09 10:06
    关注

    AJAX requests could fail for any reason and you could work around this by giving the user the option to re-submit and on server-side you do whatever checking to avoid duplicates..

    Or you can use timeouts, check this http://api.jquery.com/jQuery.ajax/

    • Timeouts for JSONP requests are available from v1.5
    评论

报告相同问题?