前端http获取状态码为 -1,而不是500等状态。
可在github上找到相关问题:
Angular 1.4.6 sporadic $http errors with status -1:
$http -1 status return on 404/500 OPTIONS :
文档说明:
第一种、是因为当请求中止或超时时,请求将作为错误返回状态 -1。
angular的开发者say:the xhr spec defines this behavior. Failed options requests always return -1, theres nothing we can do in Angular about this. You can try the same with a plain XMLHttpRequest() and you should get the same behavior.
第二种、是跨站点请求时,未添加header:"Access-Control-Allow-Origin"。
一些200、400、401、404的响应状态都能成功获取,但是当状态为500时,获取的状态为 -1 。
Object {data: null, status: -1, config: Object, statusText: ""}
在postman和浏览器中,看到它们返回的状态码是500,并且提示:
XMLHttpRequest cannot load http://localhost:3000/users. No Access-Control-Allow-Origin header is present on the requested resource. Origin http://localhost:3000 is therefore not allowed access. The response had HTTP status code 500。
显然在postman和浏览器中,它们都能从服务器获取状态码,但是它去没有出现在Angularjs。
When doing cross-site requests, the server has to add to ALL responses a valid CORS header. This includes error responses like 500.
Make sure you can see the "Access-Control-Allow-Origin" header in your 500 response, its most likely missing.
