【问题标题】:Success callback on $http.post in AngularJS using Firefox使用 Firefox 在 AngularJS 中成功回调 $http.post
【发布时间】:2013-06-06 14:05:57
【问题描述】:

我在 AngularJS 应用程序中有一个简单的 CORS AJAX 调用,并带有成功回调:

$http({method:'POST',url:"http://0.0.0.0:4567/authenticate", 
 params: {Lusername:scope.Lusername,Lpassword:scope.Lpassword}})
 .success(function(){alert("Success")})

在 Safari 中使用时效果很好:返回预期的 JSON 对象并显示警告框。但是在 Firefox 中,虽然正确返回了 JSON 对象,但不会触发成功回调。

知道为什么吗?

【问题讨论】:

  • 你有一个可以重现问题的 jsFiddle 吗?顺便说一句,我知道这只是示例,但考虑使用 $window.alert() 而不仅仅是 alert() (其中$window 是您注入的服务)。
  • CORS 的可能性。检查一下。

标签: ajax angularjs post cors


【解决方案1】:

确保您在服务器中处理 OPTIONS 请求。如果它返回 404,那么 Firefox 将不会调用下一个请求(在您的情况下是上面提到的 POST)。

【讨论】:

  • 如果我无法访问服务器选项怎么办 :)
  • 需要在服务器上设置CORS。
【解决方案2】:

用最新版本的 AngularJS 试试这个:

$http.post("http://0.0.0.0:4567/authenticate", {
  Lusername: $scope.Lusername, 
  Lpassword: $scope.Lpassword
}).success(function(data, status, headers, config) {
   alert("Success");
});

【讨论】:

  • 这并不能解释问题或帮助将来可能遇到类似问题的任何人。
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2013-09-14
  • 2012-12-27
  • 2018-03-30
  • 1970-01-01
相关资源
最近更新 更多