[英]Basic authentication external rest api from jQquery
我在尝试构建的单页应用程序中访问外部api时遇到问题。
我可以使用用户名和密码从Postman登录,然后获得AuthenticationTicket,可用于第二次请求的个人数据。
当我尝试从应用程序登录时,我得到“所请求的资源上没有'Access-Control-Allow-Origin'标头。”
我不能使其与jsonp一起使用,因为它必须发布logindata ?!
$.ajax
({
beforeSend: function (x) {
x.setRequestHeader('Authorization', 'Basic Username=x&Password=y');
},
type: "GET",
url: "https://test.com/api/login/",
dataType: 'json',
async: false,
success: function () {
alert('you're in!');
}
});
邮递员api的回应:
AuthenticationTicket → x
Cache-Control →no-cache
Content-Length →52
Content-Type →application/json;
charset=utf-8
Date →Sun, 05 Jan 2014 20:29:36 GMT
Expires →-1
LogoutKey → x
Pragma →no-cache
Server →Microsoft-IIS/7.5
SessionTicket → x
X-AspNet-Version → X-AspNet-Version 4.0.30319
X-Powered-By → X-Powered-By
Specifies the technology (ASP.NET, PHP, JBoss, e.g.) supporting the web application (version details are often in X-Runtime, X-Version, or X-AspNet-Version) ASP.NET
尝试使用JSONP的dataType:
dataType: 'jsonp',
当然,C#可以解决问题。
WebRequest req = WebRequest.Create(@"https://url.com/api/login/?param=value¶m2=value");
req.Method = "GET";
req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("Username:Password"));
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.