我正在尝试向Cloudflare的API发送GET请求。我还需要发送一些标头,X-Auth-Email
和X-Auth-Key
。当我添加这些内容时,无论是使用headers
作为对象还是在beforeSend: function(xhr)
内,Cloudflare仍然会给我一个错误,说我错过了标题。
这就是cURL请求的样子(并且有效):
curl -s -X GET "https://api.CloudFlare.com/client/v4/zones?name=DOMAIN.COM&status=active&page=1&per_page=20&order=status&direction=desc&match=all" \
-H "X-Auth-Email: EMAIL@EMAIL.COM" \
-H "X-Auth-Key: 1234567890" \
-H "Content-Type: application/json"
这就是我$.ajax()
的样子(是的,默认为GET):
$.ajax({
url: "https://api.CloudFlare.com/client/v4/zones?name=DOMAIN.COM&status=active&page=1&per_page=20&order=status&direction=desc&match=all",
dataType: "jsonp",
contentType: 'application/json',
headers: {
"X-Auth-Email": "EMAIL@EMAIL.COM",
"X-Auth-Key": "1234567890",
"Content-Type": "application/json"
},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Auth-Email", "EMAIL@EMAIL.COM");
xhr.setRequestHeader("X-Auth-Key", "1234567890");
xhr.setRequestHeader("Content-Type", "application/json");
}
}).done(function(data) {
console.log(data);
});
当我发送请求时,这是我得到的输出:
{
"success": false,
"errors": [{
"code": 9106,
"message": "Missing X-Auth-Email header"
}, {
"code": 9107,
"message": "Missing X-Auth-Key header"
}],
"messages": [],
"result": null
}
我在这里做错了什么?
答案 0 :(得分:0)
我认为你要设置两次标题。你应该使用"标题"或者" beforeSend"设置标题,而不是同时设置标题。