我正在使用https://github.com/ServiceStack/ServiceStack/wiki/New-Api中描述的方法来启用CORS。这似乎有效,即我在响应中得到了正确的Access-Control标头,但状态代码是404.这有关系吗?
为了完整性,我宁愿返回非错误代码。阅读http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html似乎我应该返回200.这是最好的方法吗?
服务方法定义是: -
[EnableCors]
public void Options(ApiKeyRequest apiKeyRequest)
{
}
HTTP请求是: -
OPTIONS http://myreallycoolwebapi/apikeyrequests HTTP/1.1
Host: myreallycoolwebapi
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Access-Control-Request-Headers: origin, content-type, accept
Accept: */*
Referer: http://localhost:8000/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
,回复是: -
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 17 Nov 2012 18:06:01 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 3
Connection: keep-alive
Cache-Control: private
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
404
这是我的资源类型。
[Route("/apikeyrequests", "POST")]
public class ApiKeyRequest : IReturn<ApiKeyRequest>
{
public string EmailAddress { get; set; }
}
POST请求工作正常。
答案 0 :(得分:3)
我仍然没有看到任何与预期路线/apikeyrequests
匹配的内容。
您是否有自定义路线定义,例如:
[Route("/apikeyrequests")]
public class ApiKeyRequest {}
为请求添加了?
答案 1 :(得分:2)
CORS规范本身明确指出非200预检应答是一个错误。请参阅&#34;如果响应的HTTP状态代码不是200&#34;部分:
http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0
我不知道这个要求的动机是什么。也许是因为404可能会引起关于端点是否确实存在的混淆。