Javascript Ajax 响应 200 正常,但显示无法加载响应数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32821289/
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
Ajax Response 200 ok, but shows failed to load response data
提问by Param Singh
I am making an ajax call to a backend rest api, the api is returning fine. If I console.log() the success data and error data, it gives "resource logged in", 200 ok on the console but when I view it in the network tab response for that auth/login route, it shows "Failed to load the response data". And this happens sometimes only and not always. Why? Here's the snippet of my ajax call.
我正在对后端 rest api 进行 ajax 调用,api 返回正常。如果我 console.log() 成功数据和错误数据,它会在控制台上提供“资源登录”,200 ok,但是当我在该身份验证/登录路由的网络选项卡响应中查看它时,它显示“加载失败响应数据”。这种情况有时会发生,但并非总是如此。为什么?这是我的 ajax 调用的片段。
ajax
.post('auth/login', {
data: {
oauth_provider: 'google',
oauth_token: (isToken ? authResult : authResult.access_token)
},
cache: false
})
.done(function(data) {
console.log(data); // Resource Logged in
})
.error(function(err){
console.log(err);
})
Here's the content of my ajax.js
这是我的 ajax.js 的内容
define(
[
'jquery',
'util',
],
function ($, util) {
var ajax = {
request: function (type, url, options) {
if (url.indexOf('http') === -1) {
url = util.url(url);
}
if (options === undefined) {
options = {};
}
options.type = type
options.url = url;
return $.ajax(options);
},
get: function (url, options) {
return ajax.request('GET', url, options);
},
post: function (url, options) {
return ajax.request('POST', url, options);
},
put: function (url, options) {
return ajax.request('PUT', url, options);
},
delete: function (url, options) {
return ajax.request('DELETE', url, options);
}
};
return ajax;
}
)
回答by Param Singh
Apparently, it turns out that there's some problem with the clearing up the cookies. On clearing up them , the system behaves fine. Not sure need help!
显然,事实证明清除 cookie 存在一些问题。清除它们后,系统运行良好。不确定需要帮助!
回答by dhilipsiva
This is a duplicate of: Chrome dev tools fails to show response even the content returned has header Content-Type:text/html; charset=UTF-8
这是以下内容的重复:即使返回的内容具有标题 Content-Type:text/html,Chrome 开发工具也无法显示响应;字符集=UTF-8
For anyone coming here in the future, here is your answer: https://stackoverflow.com/a/38925237/1235072
对于将来来到这里的任何人,这是您的答案:https: //stackoverflow.com/a/38925237/1235072
More info: https://bugs.chromium.org/p/chromium/issues/detail?id=141129
更多信息:https: //bugs.chromium.org/p/chromium/issues/detail?id=141129
回答by Mohammad AlBanna
I confirm this. Since Chrome version 45 and I see some of my Ajax requests got 200 as a status code but a problem in showing the content "Failed to Load Content".
我确认这一点。由于 Chrome 版本 45,我看到我的一些 Ajax 请求将 200 作为状态代码,但在显示“无法加载内容”内容时出现问题。
My Ajax requests are inside for loop, request the failed contents again makes the content to be loaded fine.
我的 Ajax 请求在 for 循环内,再次请求失败的内容使内容加载正常。
It seems to solve that inside the for loop is using setTimeout
between request and the other.
似乎解决了 for 循环内部setTimeout
在请求和另一个之间使用的问题。
回答by Nupur
Because of postback this was happening in my project. When I solved the postback issue,the error gone.
由于回发,这发生在我的项目中。当我解决回发问题时,错误消失了。