如何在CORS上启用Angular $ http重定向

时间:2015-02-27 00:40:51

标签: angularjs express cors

我有一个启用了CORS的节点/表达式

当我对我的应用发送/login时会重定向到/failure/success

但总是得到一个

XMLHttpRequest cannot load http://exampledomain.com/login. The request was redirected to 'http://exampledomain.com/success', which is disallowed for cross-origin requests that require preflight.

$http向服务器发出两个请求

OPTIONS请求

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://otherdomain.com
Vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,PUT,POST,PATCH,DELETE
Access-Control-Allow-Headers: accept, content-type
Connection: keep-alive

选项回复

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://otherdomain.com
Vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,PUT,POST,PATCH,DELETE
Access-Control-Allow-Headers: accept, content-type
Connection: keep-alive

POST请求

POST /admin/login HTTP/1.1
Host: exampledomain.com
Connection: keep-alive
Content-Length: 43
Accept: application/json, text/plain, */*
Origin: http://otherdomain.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2267.0 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://otherdomain.com
Accept-Encoding: gzip, deflate
Accept-Language: es-419,es;q=0.8

POST回复

HTTP/1.1 302 Moved Temporarily
Vary: X-HTTP-Method-Override, Origin, Accept
Access-Control-Allow-Origin: http://otherdomain.com
Access-Control-Allow-Credentials: true
Location: /success
Content-Type: text/plain; charset=utf-8
Content-Length: 45
set-cookie: cookie-data; Path=/; HttpOnly
Connection: keep-alive

我认为标题是正确的,但Location标题缺少一些内容。可以帮助我这个

我正在使用$ http执行请求,如下所示

$http.defaults.useXDomain = true;
$http.post('http://exampledomain/login', {
  username: 'user',
  password: 'pass'
}).success(...).error(...)

但成功永远不会被称为

1 个答案:

答案 0 :(得分:-1)

http://exampledomain.com未启用CORS,因此无法向其发出请求。事实上,浏览器会进行预检检查,因为它失败了,所以请求实际上并没有发送到服务器。

更改此选项的唯一方法是在您的域的服务器上启用CORS。