Jquery ajax 相当于 Angular 中的 withcredentials
Jquery ajax equivalent of withcredentials in Angular
我目前正在使用 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 Unauthorized --
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);
}
});
我目前正在使用 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 Unauthorized --
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);
}
});