如果内容为空,则jQuery ajax调用返回空错误 [英] jQuery ajax call returns empty error if the content is empty

查看:129
本文介绍了如果内容为空,则jQuery ajax调用返回空错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次在res.reply = 2时都调用getResult()函数,但是在某些情况下res为空.当返回值为空时,调用console.log("error").这适用于 jQuery Mobile 的旧版本.现在的版本是 1.3.2 .

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();
            }         
        }
    });
}

推荐答案

dataType: "json"

意味着:给我json,别无其他.一个空字符串不是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 
        }
    }
});

这篇关于如果内容为空,则jQuery ajax调用返回空错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆