我目前正在使用angular向我的本地主机CouchDB发送HTTP GET请求。我已经通过AngularJS做到了这一点:
var req =
{
method: 'POST',
url: BASEURL+'_session',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'name=' + APIKEY + '&password=' + APIPASSWORD,
withCredentials: true
}
$http(req).then
(function(result) {console.log(result.data);},
function(error) {console.log(error)});
并且只是尝试使用AJAX(我是绝对的初学者)来做同样的事情。我在ajax中似乎看不到等效版本的“ withcredentials”……有人可以帮忙吗?无论哪种方式,我都会得到401未经授权-
Failed to load resource: the server responded with a status of 401 (Unauthorized)
这是我到目前为止的内容:
-编辑-
var jqxhr = $.ajax({
url: BASEURL+'_session',
headers: {"Content-Type": "application/x-www-form-urlencoded"},
data: 'name=' + APIKEY + '&password=' + APIPASSWORD,
type: "POST",
success: function(response)
{
alert('Success!' + response);
var jqxhr2 = $.ajax({
url: BASEURL+DATABASE+'/users',
type: "GET",
success: function(response)
{
alert('Success!' + response);
}
});
}
});
您可以设置withCredentials
到true
了跨域请求如下:
$.ajax({
url: BASEURL+DATABASE+'/'+'users',
contentType: "application/x-www-form-urlencoded",
data: 'name=' + APIKEY + '&password=' + APIPASSWORD,
type: "POST",
xhrFields: { withCredentials: true },
success: function() {
alert('Success!' + authHeader);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句