问题描述
我正在尝试从外部API(来自Mashape)获取一些数据,这些数据需要特定的标头来设置API密钥。
I'm trying to get some datas from an external API (from Mashape) that requires a specific header to set the API key.
使用jQuery一切正常:
Everything is ok using jQuery :
$.ajax({
url: 'https://omgvamp-hearthstone-v1.p.mashape.com/cardbacks',
type: 'GET',
data: {},
dataType: 'json',
success: function(data) { console.dir((data.source)); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "MY_API_KEY");
}
});
然而,当我尝试用axios为反应应用程序做同样的请求时,我有404错误:
However, when I'm trying to do the same request with axios for a react application, I have a 404 error:
axios.get({
url: 'https://omgvamp-hearthstone-v1.p.mashape.com/cardbacks',
headers: {
"X-Mashape-Authorization": "MY_API_KEY"
}
})
我有什么遗漏的吗?谢谢。
Is there something I'm missing ? Thanks.
推荐答案
尝试这样做而不设置默认值:
Try also to do it like this without setting defaults:
axios.get('https://omgvamp-hearthstone-v1.p.mashape.com/cardbacks', {
headers: { "X-Mashape-Key": "MY_API_KEY" }
})
.then((resp) => {
console.dir(resp);
})
.catch(err => {
console.log(err);
});
}
它应该有效。
PS我还观察到您使用了不同的标题键(X-Mashape-Authorization)和答案(X-Mashape-Key)。也许这也与404错误有关?
PS I also observed that you use different header keys in question(X-Mashape-Authorization) and answer(X-Mashape-Key). Maybe that is also somehow related to 404 error?
这篇关于我不能做一个需要用axios设置标头的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!