jQuery Ajax 调用触发错误事件但返回 200 ok

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16230624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:44:52  来源:igfitidea点击:

Ajax call fires error event but returns 200 ok

javascriptjsonapijquery

提问by user2314110

$.ajax({
        url: 'http://intern-dev01:50231/api/language',
        type: 'GET',
        dataType: 'json',
        success: function() {
            console.log('It Works!');
        },
        error: function (request,status, error) {
            console.log(error);
            alert(status);
        }
    });

Why do this ajax call not work ?? if i call in browser it works fine :/.

为什么这个ajax调用不起作用?如果我在浏览器中调用它工作正常:/。

This is what fiddler returns:

这是 fiddler 返回的内容:

HTTP/1.1 200 OK
Content-Length: 122
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 26 Apr 2013 06:56:40 GMT

[{"LanguageId":1,"LanguageName":"Dansk"},{"LanguageId":2,"LanguageName":"Tysk"},{"LanguageId":3,"LanguageName":"Engelsk"}]

回答by Bharat Chodvadiya

You have to check ajax response if it is valid or not. When you specify in ajax:

您必须检查 ajax 响应是否有效。当您在 ajax 中指定时:

dataType: 'json',

jQuery will fire the error event if the response cannot be parsed as JSON, even if server returns 200 OK. Check the data returned from the server and make sure it is valid JSON (try JSONLint service).

如果响应无法解析为 JSON,jQuery 将触发错误事件,即使服务器返回 200 OK。检查从服务器返回的数据并确保它是有效的 JSON(尝试 JSONLint 服务)。

If the returned data is not JSON or it has syntax errors then fix them in your server side code. You can just return {} from the server side script.

如果返回的数据不是 JSON 或者它有语法错误,那么在你的服务器端代码中修复它们。您可以从服务器端脚本返回 {}。

Also try this.

也试试这个。

$.ajax({
    url: 'http://intern-dev01:50231/api/language',
    type: 'GET',
    cache: false,        
    complete: function (xhr, status) {
      if (status === 'error' || !xhr.responseText) {
          console.log(error);
          alert(status);
      }
      else {
       console.log('It Works!');.
      }
    }        
});

回答by SanTheta

There is a parsing error since the status shows 200 OK. The problem lies in the datatype:json. To test this, remove the line and it should work. In order to fix this, you can change it to the datatype:text. See this link too for similar question

由于状态显示 200 OK,因此存在解析错误。问题在于数据类型:json。要测试这一点,请删除该行,它应该可以工作。为了解决这个问题,您可以将其更改为数据类型:文本。有关类似问题,请参阅此链接

回答by dush88c

If you are testing locally with a different web app and web API applications , then debug your application and test API send data correctly and app calls to API via AJAX and return data.

如果您使用不同的 Web 应用程序和 Web API 应用程序在本地进行测试,则调试您的应用程序和测试 API 正确发送数据,应用程序通过 AJAX 调用 API 并返回数据。

since domains are not similar when run application AJAX call doesn't hit to success function. because browser prevents Cross Site request. If you publish both app in local and debug , it's work fine.

因为当运行应用程序 AJAX 调用没有达到成功功能时域不相似。因为浏览器阻止 Cross Site request. 如果您在 local 和 debug 中同时发布应用程序,则可以正常工作。

hope this would be helpful someone.

希望这会对某人有所帮助。

回答by DaveS

Check the url parameter and make sure its the same as the loaded page. You might be doing a cross-domain ajax call. If you were wanting to make a cross-domain ajax call, notice that the only dataTypes allowed to make cross-domain requests are "script" and "jsonp".

检查 url 参数并确保它与加载的页面相同。您可能正在执行跨域 ajax 调用。如果您想进行跨域 ajax 调用,请注意允许进行跨域请求的唯一数据类型是“script”和“jsonp”。

Ran into this issue in a dev environment where the URL was an IP address and the page loaded a domain-name pointing to that ip.

在 URL 是 IP 地址并且页面加载了指向该 IP 的域名的开发环境中遇到此问题。

回答by Senju

I know I'm a little late, but I just ran into the same problem and this is one of the top search results on Google. I managed to fix it by moving datatype above url like this:

我知道我有点晚了,但我刚刚遇到了同样的问题,这是 Google 上最热门的搜索结果之一。我设法通过将数据类型移动到 url 上方来修复它,如下所示:

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'http://intern-dev01:50231/api/language',
    success: function() {
        console.log('It Works!');
    },
    error: function (request,status, error) {
        console.log(error);
        alert(status);
    }
});