DART HttpRequest不为CORS OPTIONS请求提供授权标头

时间:2015-02-03 15:56:40

标签: dart cors dart-html

我尝试使用HttpRequest(dart:html)执行POST请求以调用使用基本身份验证保护的休息服务。

HttpRequest request = new HttpRequest(); // create a new XHR
var url = "http://localhost:8082/xyz";
request.open("POST", url, async: false); // POST the data to the server

String username = "foo";
String password = "bar";
final auth = CryptoUtils.bytesToBase64(UTF8.encode("$username:$password"));

request.setRequestHeader('authorization',"Basic $auth");
request.setRequestHeader('content-type',"application/json");
request.setRequestHeader("accept", "application/json");
String jsonData = '{"language":"dart"}'; // etc...

request.send(jsonData);  //exception 401 Unauthorized


在执行POST调用之前,执行OPTIONS调用(由dart:html发出),没有授权标头。这导致401 Unauthorized响应。

  

请求标题:

     
    

接受:* / *
    接受编码:     gzip的,放气,SDCH
    接受语言:     的en-US,连接; Q = 0.8
    访问控制请求报头:     授权,内容类型,接受
    访问控制请求,方法:     POST
    缓存控制:     最大年龄= 0
    连接:     保活
    主办:     本地主机:8082
    起源:     http://localhost:63342
    引用站点:     http://localhost:63342/dart_client/test/all_test.html
    用户代理:     Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_5)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 38.0.2125.0(Dart)Safari / 537.36

  
     

回复标题:

     
    

的Content-Length:     0
    日期:     星期一,02二月2015 23:33:58 GMT
    服务器:     码头(9.2.7.v20150116)
    WWW身份验证:     基本领域=" xyzrealm"

  


有没有办法为OPTIONS调用提供授权标头?

任何建议都会很棒。感谢

1 个答案:

答案 0 :(得分:1)

OPTIONS请求由浏览器自动生成,您无法修改该请求。服务器需要允许OPTIONS预检请求而无需身份验证。

参见http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0 用户凭据被排除在外。

相关问题