【问题标题】:Angular JS: http.post is returning null in Internet ExplorerAngular JS:http.post 在 Internet Explorer 中返回 null
【发布时间】:2016-08-15 22:07:40
【问题描述】:

我正在从 Angular JS 进行 ajax 调用:

var response = $http.post(
    '/services/login/?_nochache='+new Date().getTime(),      
    JSON.stringify(credentials)
);

我正在添加 _nocache 设置,认为可能是一些缓存或类似的东西。

我还将对象 credentials 转换为字符串,认为 Internet Explorer 无法识别该对象。

我真的迷路了,在 chrome 中调用完美,在 IE 10 中,服务的响应为空。

这是什么原因造成的?

编辑

服务是返回401,没关系,因为用户错误,但是响应应该是(就像在其他浏览器中一样),表示用户错误的错误字符串,在这个案例为空。

我正在使用这样的承诺:

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": ""
}

编辑

这是我在 IE 中获得的响应正文。

这些是我使用401 得到的标头,这是正确的,但响应正文是错误的。

【问题讨论】:

  • 你是如何使用承诺响应的?请张贴代码。
  • 使用IE10时是否到达后端服务(services/login)?
  • 我猜如果没有到达后端我不会有 401 对吧?
  • 你试过 $httpParamSerializerJQLike 吗?有几天我遇到了类似的问题,docs.angularjs.org/api/ng/service/$httpParamSerializerJQLike
  • 我有同样的错误:/

标签: javascript angularjs ajax internet-explorer


【解决方案1】:

这似乎和我的问题一样。

就我而言,我的请求功能在除 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 中的一种策略处理缓存发生的。

当我使用本地服务器发送请求或获取响应时,可能会发生这种情况。

在其他非本地服务器上测试时它会消失。

我希望你的问题和我的一样。

【讨论】:

    【解决方案2】:

    这是一种延伸,如果我在这里错了,请不要发疯。没有代表点可以发表评论。

    你看过这篇文章吗? Error 401 and 403 only in Internet Explorer (not in Chrome, Firefox, ..) - IIS configuration?

    【讨论】:

    • 您似乎遇到了授权问题,IE 是授权阻止程序的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多