【发布时间】:2015-04-02 21:42:58
【问题描述】:
我尝试使用 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
接受语言: zh-CN,zh;q=0.8
访问控制请求标头: 授权、内容类型、接受
访问控制请求方法: 发布
缓存控制: 最大年龄=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, like Gecko) Chrome/38.0.2125.0 (Dart) Safari/537.36响应头:
内容长度: 0
日期: 格林威治标准时间 2015 年 2 月 2 日星期一 23:33:58
服务器: 码头(9.2.7.v20150116)
WWW-认证: 基本领域="xyzrealm"
有没有办法为 OPTIONS 调用提供授权标头?
任何建议都会很棒。谢谢
【问题讨论】: