17

I call the getResult() function everytime when res.reply = 2, but there are cases that res is empty. When the returned value is empty console.log("error") is invoked. This works in older versions of jQuery Mobile. Now the version is 1.3.2.

function getResult()
{
    request = $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: {
            ....
        },
        error: function() {         
            console.log("error");
        },
        success: function(res) {
            if(res.reply=='2') {
                getResult();
            }         
        }
    });
}
8
  • 1
    Expecting help with jQuery 1.3.2 is a little... optimistic. Why don't you use a current version? Commented Aug 14, 2013 at 12:08
  • Hitting the error handler does not necessarily mean the response is empty - it most commonly means that a response could not be retrieved due to an error on the server. Check the network traffic in Firebug and your server logs for an error. Commented Aug 14, 2013 at 12:11
  • 1
    What error you are receiving ..you can check with error : function (xhr,err){ console.log(xhr);console.log(err); } Commented Aug 14, 2013 at 12:11
  • Because it is the latest version jquerymobile.com Commented Aug 14, 2013 at 12:24
  • console.log(xhr);console.log(err); returns Object { readyState=4, status=200, statusText="OK"} search...mbers=1 (line 136) parsererror Commented Aug 14, 2013 at 12:27

2 Answers 2

29
dataType: "json"

means: give me json, nothing else. an empty string is not json, so recieving an empty string means that it wasn't a success...

request = $.ajax({
    type: "POST",
    url: url,
    data: {
        ....
    },
    error: function() {         
        console.log("error");
    },
    success: function(res) {
        var response = jQuery.parseJSON(res);
        if(typeof response == 'object'){
            if(response.reply == '2') {
                getResult();
            }  
        } else {
              //response is empty 
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like normally you do want a JSON response, so I wouldn't change your dataType to "text", instead I would get the server to return a valid JSON response even when the response is empty e.g. "{}" instead of "".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.