【问题标题】:Javascript - Skips success when getting statusJavascript - 获取状态时跳过成功
【发布时间】:2018-01-18 18:14:15
【问题描述】:

我正在编写一个小代码,试图获取服务器 http 状态的返回

目前我已经制定了以下代码(有效),但我想知道为什么调用会跳过成功并直接出错。

这是我目前拥有的代码:

$(document).ready(function(){
        $("#test-button").on("click", function() {
            $.ajax({
                type: "GET",
                dataType: 'jsonp',
                url: "<<INSERT THE URL>>",
                success: function(response) {
                    alert(response.status);
                },
                error: function(response) {
                    if(response.status == 200) {
                        // Redirecting
                        window.location.href = '<<INSERT THE URL>>';
                    } else {
                        alert('The organisation you entered is not available');
                    }
                }
            });
        });
    });

基本上我只想知道为什么它会跳过成功,即使得到 200 作为回报。

谢谢

【问题讨论】:

标签: javascript ajax


【解决方案1】:

如果您来自服务器的响应无效jsonp,则可能会发生这种情况。 documentation 声明:

将响应评估为 JSON 并返回一个 JavaScript 对象。 (...) 对 JSON 数据进行严格解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多