Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax call returns empty error if the content is empty

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();
            }         
        }
    });
}
like image 569
user2632980 Avatar asked Aug 14 '13 12:08

user2632980


1 Answers

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 
        }
    }
});
like image 135
Mr.Manhattan Avatar answered Nov 08 '22 12:11

Mr.Manhattan