【问题标题】:jQuery - Alert Based on JSON Response DatajQuery - 基于 JSON 响应数据的警报
【发布时间】:2015-01-17 17:19:38
【问题描述】:

我需要根据 JSON 响应中出现的字符串显示警报。 这是我的代码:

$.ajax({
    method: 'POST',
    url: dataString2,
    data: dataString,
    headers: {
        "Authorization": "Basic " + btoa('' + Username + '' + ":" + '' + Password + '')
    },
    success: function(jqXHR) {
        if (jqXHR.responseJSON('ok')) {
            alert('First Real Server Configured');
        }
    },
    statusCode: {
        406 : function() {
            alert('There is an unexpected string in your data.\nFix the error and try again.');
        },
        401 : function() {
            alert('Wrong username or password.');
        }
    },
});
});

这是 JSON 响应:

{
     "status":"ok"
}

HTTP 响应码为“200 OK”,服务器响应 Content-Type:application/json

当使用上面的代码时,我得到: Uncaught TypeError: undefined is not a function

有什么想法吗?

【问题讨论】:

  • 好吧,究竟什么是“不是函数”?哪条线?
  • 代码的哪一部分未定义?
  • jqXHR 将是响应,不会有 responseJSON 属性。要访问 jqXHR 对象,请在 success 处包含 (data, textStatus, jqXHR),然后在 jqXHR.responseJSON 处访问 responseJSON - 不包含 ("ok")
  • @Juhana,如下: if (jqXHR.responseJSON('ok')) {
  • @Guest271314 但只有当响应数据中出现字符串“ok”时,我才需要显示警报。

标签: javascript jquery ajax json


【解决方案1】:

试试

$.ajax({
    method: 'POST',
    url: "/echo/json/",
    data: {json:JSON.stringify({status:"ok"})},
    dataType: "json",
    headers: {
        "Authorization": "Basic " 
        + btoa('' + "Username" + '' + ":" + '' + "Password" + '')
    },
    success: function(data, textStatus, jqXHR) {
        // check `jqXHR.responseJSON` here
        if (jqXHR.responseJSON.status === "ok") {
            alert('First Real Server Configured');
        }
    },
    statusCode: {
        406 : function() {
            alert('There is an unexpected string in your data.'
                 + '\nFix the error and try again.');
        },
        401 : function() {
            alert('Wrong username or password.');
        }
    }
});

jsfiddle http://jsfiddle.net/ca9aa04r/

【讨论】:

  • 似乎当我使用不同于“ok”的字符串(但仍然是响应数据的一部分)时,不会弹出警报。例如,我的服务器响应是:{ "status":"err", "message":"Alteon cannot create the specified new entry. An entry with the same specifications already exists." } 当我使用时:`success: function(data, textStatus, jqXHR) { // 在这里检查 jqXHR.responseJSON if (jqXHR.responseJSON.status === "already exists") { alert('真实服务器已经存在'); } }, 不显示警报。
  • @UdiDahan 如果可能,可以创建 stacksn-ps / fork jsfiddle jsfiddle.net/ca9aa04r ,包含 js ,来演示?
  • 你可以在我使用的链接中看到jqXHR.responseJSON.data 但我也尝试过jqXHR.responseJSON.status 就像你写的那样,它没有工作。
  • 参见console at jsfiddle “加载资源失败:服务器响应状态为 404(未找到)=> fiddle.jshell.net/yym93j7y/show/alteon1.js " 可以发布完整的urlalteon1.js 吗?
  • 它目前位于我的 PC 本地,因此有点问题。无论我在 jqXHR.responseJSON.data 中写什么,它似乎都会弹出第一个警报。我写了 jqXHR.responseJSON.data = 'bla',它仍然会弹出警报(虽然我可以向你保证,响应数据中没有“bla”字符串)。
【解决方案2】:

我找到了解决办法:

$.ajax({
                                        type: 'POST',
                                        url: dataString2,
                                        data: dataString,
                                        contentType: 'application/json',
                                        headers: {
                                            "Authorization": "Basic " + btoa('' + Username + '' + ":" + '' + Password + '')
                                        },
                                        success: function(data) {
                                                status = data.status;
                                                message = data.message;
                                                if (status === 'ok') {
                                                alert("First Real Server Configured Successfully");
                                                }
                                                if (status === 'err') {
                                                alert(message);
                                                }
                                            },
                                        statusCode: {
                                            406 : function() {
                                                alert('There is an unexpected string in your data.\nFix the error and try again.');
                                            },
                                            401 : function() {
                                                alert('Wrong username or password.');
                                            }
                                            },
                                });

来源: $.jquery ajax returned data (json) displays as 'undefined'

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多