Angular $ http状态(-1) [英] Angular $http status (-1)

查看:131
本文介绍了Angular $ http状态(-1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过AngularJs $ HTTP GET 调用后端服务/资源。当它不可用时,它返回 404 ,Chrome网络标签显示它是 404 ,如下所示:

I call a backend service/resource by AngularJs $HTTP GET. When it is not available it returns 404 and Chrome Network tab shows that it is 404 as the followings:

Request Method:OPTIONS
Status Code:404 Not Found

但是在AngularJs $ HTTP 调用结果中,我得到 status = -1 而不是 404

BUT inside AngularJs $HTTP call result, I get status=-1 and not 404:

{data: null, status: -1, config: Object, statusText: ""}

为什么会发生?

NB)
我在后端有一个码头服务并启用了CORS:

NB ) I have a jetty Service on the backend and enabled CORS:

"Access-Control-Allow-Origin":"*"
"Access-Control-Allow-Headers": "origin, content-type, accept, authorization"
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD"


推荐答案

它返回-1,因为跟踪显示的是,甚至没有发送GET请求。已发送的是飞行前OPTIONS请求,通过返回404,阻止浏览器发送实际的GET请求。

It returns -1 because, as the traces show, the GET request has not even been sent. What has been sent is the pre-flight OPTIONS request, which, by returning 404, prevented the browser to send the actual GET request.

您的服务器不应返回对OPTIONS请求的404响应。它应该返回200响应。

Your server shouldn't return a 404 response for the OPTIONS request. It should return a 200 response.

这篇关于Angular $ http状态(-1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆