【发布时间】:2013-08-18 08:17:53
【问题描述】:
我正在尝试从我的 API 发布数据,但我无法通过基本身份验证。
我试试:
$.ajax({
type: 'POST',
url: http://theappurl.com/api/v1/method/,
data: {},
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Basic [REDACTED]');
}
});
我的服务器配置响应是:
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
我得到的标题是:
请求标头
OPTIONS /api/v1/token-auth/ HTTP/1.1
Host: theappurl.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Access-Control-Request-Headers: origin, authorization, content-type
Accept: */*
Referer: http://127.0.0.1:8080/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
响应标头
HTTP/1.1 401 Unauthorized
Server: nginx/1.1.19
Date: Fri, 16 Aug 2013 01:29:21 GMT
Content-Type: text/html
Content-Length: 597
Connection: keep-alive
WWW-Authenticate: Basic realm="Restricted"
我猜服务器配置不错,因为我可以从 Advanced REST Client(Chrome 扩展)访问 API
有什么建议吗?
PD: 我从高级 REST 客户端获得的标头是:
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Authorization: Basic [REDACTED]
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
和
Server: nginx/1.1.19
Date: Fri, 16 Aug 2013 01:07:18 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept, Cookie
Allow: POST, OPTIONS
X-Robots-Tag: noindex
发送OPTION方法
【问题讨论】:
-
我意识到这篇文章早已死去,但我只是想指出,以防您不知道通过发布您的 Authorization: 标头,您实际上已经清楚地发布了您的密码。那里的乱码字符串只是您的用户名:密码的base64编码,因此每个人都可以看到您的密码。希望您意识到这一点并在这里使用了一个虚拟密码:)
-
这适用于 ssrs 报告服务器 2017。它隐藏了 URL 中的密码和用户名。
-
@Lexelby:用户名是“用户”,密码是西班牙语的“和密码”。所以我猜这些不是真正的凭据。
标签: jquery ajax django api cross-domain