我正在从 Angular JS 进行 ajax 调用:
var response = $http.post(
'/services/login/?_nochache='+new Date().getTime(),
JSON.stringify(credentials)
);
我正在添加 _nocache
设置,认为可能是一些缓存或类似的东西。
我还在将对象 credentials
转换为字符串,认为 Internet Explorer 无法识别该对象。
我真的迷路了,在 chrome 中调用完美,在 IE 10 中,服务的响应为空。
这可能是什么原因造成的?
编辑
服务返回 401,这没问题,因为用户错了,但响应应该是(就像在其他浏览器中一样),错误字符串表明用户错了,在这个case 为空。
我正在使用这样的 promise :
promise.then(onLoginOk, onLoginError);
...
function onLoginError(response) {
console.log(JSON.stringify(response));
}
控制台返回
{
"data": null,
"status": -1,
"config": {
"method": "POST",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "http://dev.site.com:8000/api/auth/login/",
"data": {
"username": "mail@domain.com",
"password": "password"
},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8"
}
},
"statusText": ""
}
编辑
最佳答案
这似乎是我的同样问题。
就我而言,我的请求函数在除 MS Edge 之外的所有浏览器中都运行良好。
我试图寻找解决方案或原因,终于找到了一个。
一些答案建议添加标签,例如
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT" />
或将代码添加到 config
中
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
但是这些都不能解决我的问题。
最后我看到一个答案说这可能是由Edge中的一种策略处理缓存引起的。
当我使用本地服务器发送请求或获取响应时,可能会发生这种情况。
在本地以外的其他服务器上测试时,它会消失。
我希望你的问题和我的一样。
关于javascript - Angular JS : http. post 在 Internet Explorer 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963783/