尝试构建一个DNN服务框架WebAPI,但是我在使用CORS时遇到麻烦.我有所有适当的标题(我想),但仍然似乎没有工作.
错误:
XMLHttpRequest cannot load http://www.dnndev.me/mysite/builder/API/echo?message=Hello+World&_=1412707749275. Request header field Key is not allowed by Access-Control-Allow-Headers.
请求标题:
Remote Address: 127.0.0.1:80 URL: http://www.dnndev.me/mysite/builder/API/echo?message=Hello Request Method: OPTIONS Status Code: 200 OK Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Access-Control-Request-Headers: accept,key Access-Control-Request-Method: GET Connection: keep-alive Host: www.dnndev.me User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/37.0.2062.124 Safari/537.36
回应标题:
Access-Control-All-Headers: Origin,X-Requested-With,Content-Type,Accept,Key Access-Control-Allow-Methods: * Access-Control-Allow-Origin: * Cache-Control: no-cache Content-Length: 13 Content-Type: application/json; charset=utf-8 Date: Tue,07 Oct 2014 18:49:10 GMT Expires: -1 Pragma: no-cache Server: Microsoft-IIS/7.5
一般来说,这个错误是由于在“访问控制全标题”中没有适当的标题引起的.但是,我正在发送正确的响应以允许ajax继续请求.它只是拒绝.
$.ajax({ type: 'GET',url: 'http://www.dnndev.me/mysite/builder/API/echo',dataType: 'json',data: { message: 'Hello' },crossDomain: true,headers: { 'Key': 'Bearer 7680ff6e-1362-4236-a9cd-c6bc8b6f13ea' },success: function (result) { console.log(result); } });