【发布时间】:2014-01-21 16:20:54
【问题描述】:
我正在向跨域资源发出一个简单的 CORS 请求。我假设它是一个带有参数的 POST 请求,它会被归类为简单的 CORS 请求,因此不需要飞行前调用。但似乎并非如此。
另外,不幸的是,因为我使用的是 .NET Web API,任何简单的数据类型都必须在 Post 请求中的查询字符串中传递。
使用 Angular $http 发布帖子。
选项:
Request URL:http://api.local.foundation.com/account/LoginAutomatically?key=null
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:api.local.foundation.com
Origin:http://www.local.foundation.com
Pragma:no-cache
Referer:http://www.local.foundation.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Query String Parametersview sourceview URL encoded
key:null
发布:
Request URL:http://api.local.foundation.com/account/LoginAutomatically?key=null
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:0
Content-Type:application/json;charset=utf-8
Host:api.local.foundation.com
Origin:http://www.local.foundation.com
Pragma:no-cache
Referer:http://www.local.foundation.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Query String Parametersview sourceview URL encoded
key:null
【问题讨论】:
-
所有 CORS 都需要预检 OPTIONS 请求。
Access-Control-*标头是客户端在继续之前查找的内容。请求就是请求,无论多么“简单”。 -
@danronmoon OPTIONS 并非所有 CORS 都需要。它仅在“非简单”CORS 请求之前发送。 CORS 规范定义了一个简单的 CORS 请求。
标签: javascript angularjs asp.net-web-api cors