如何使用header参数发送HTTP请求? [英] How to send an HTTP request with a header parameter?
问题描述
我对javascript和网络编程非常陌生,我需要一些帮助。我有一个HTTP请求,我需要通过javascript发送,并需要将输出存储在一个变量中。我尝试只使用调用网址:
I'm very new to javascript and web programming in general and I need some help with this. I have an HTTP request that I need to send through javascript and get need to store the output in a variable. I tried using just the call url:
https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015
但它返回一个身份验证错误,因为我没有发送我的API密钥而它没有告诉我如何在URL中执行此操作。 API密钥被列为标题而不是参数,我不知道该怎么做。我尝试使用XMLHttpRequest()类,但我不太确定我到底知道它做了什么,也不能让它工作。
But it returns an authentication error because I didn't send my API key and it doesn't show me how to do it just in the URL. The API key is listed as a header and not a paramater and I'm not sure what to do with that. I tried using the XMLHttpRequest() class but I'm not quite sure I understand exactly what it does nor could I get it to work.
实际的HTTP请求
GET https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015 HTTP/1.1
Host: api.fantasydata.net
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
我只需要弄清楚如何将该请求与密钥一起发送,以及如何将它返回的JSON doc存储为javascript中的变量。
I just need to figure out how to send that request along with the key and how to store the JSON doc it returns as a variable in javascript.
编辑:这是我到目前为止:
This is what I have so far:
function testingAPI(){
var key = "8a1c6a354c884c658ff29a8636fd7c18";
httpGet("https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015",key );
alert(xmlHttp.responseText);
var x = 0;
}
function httpGet(theUrl,key)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.setRequestHeader("Ocp-Apim-Subscription-Key",key);
xmlHttp.send( null );
return xmlHttp.responseText;
}
谢谢!
推荐答案
如果它说API密钥被列为标题,则很可能需要在标题
选项中设置它。你的http请求。通常是这样的:
If it says the API key is listed as a header, more than likely you need to set it in the headers
option of your http request. Normally something like this :
headers: {'Authorization': '[your API key]'}
这是另一个例子问题
$http({method: 'GET', url: '[the-target-url]', headers: {
'Authorization': '[your-api-key]'}
});
编辑刚看到您想将响应存储在变量中。在这种情况下,我可能只是使用AJAX。这样的事情:
Edit : Just saw you wanted to store the response in a variable. In this case I would probably just use AJAX. Something like this :
$.ajax({
type : "GET",
url : "[the-target-url]",
beforeSend: function(xhr){xhr.setRequestHeader('Authorization', '[your-api-key]');},
success : function(result) {
//set your variable to the result
},
error : function(result) {
//handle the error
}
});
我从这个问题,我正在工作,所以我现在无法测试它,但看起来很稳定
I got this from this question and I'm at work so I can't test it at the moment but looks solid
编辑2:非常确定您应该可以使用此行:
Edit 2: Pretty sure you should be able to use this line :
headers: {'Authorization': '[your API key]'},
第一次编辑中的 beforeSend
行。这对你来说可能更简单
instead of the beforeSend
line in the first edit. This may be simpler for you
这篇关于如何使用header参数发送HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!