[英]How to send an HTTP request with a header parameter?
I'm very new to javascript and web programming in general and I need some help with this. 我对javascript和网络编程很新,我需要一些帮助。 I have an HTTP request that I need to send through javascript and get need to store the output in a variable.
我有一个HTTP请求,我需要通过javascript发送,并需要将输出存储在一个变量中。 I tried using just the call url:
我尝试只使用调用url:
https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015
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. 但它返回一个身份验证错误,因为我没有发送我的API密钥,它没有告诉我如何只在URL中执行它。 The API key is listed as a header and not a paramater and I'm not sure what to do with that.
API密钥被列为标题而不是参数,我不知道如何处理它。 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.
我尝试使用XMLHttpRequest()类,但我不太确定我到底知道它做了什么,也不能让它工作。
The actual HTTP Request 实际的HTTP请求
GET https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015 HTTP/1.1
Host: api.fantasydata.net
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
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. 我只需要弄清楚如何发送该请求以及密钥以及如何存储它作为javascript中的变量返回的JSON文档。
EDIT: 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;
}
Thank you! 谢谢!
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. 如果它说API密钥被列为标题,则很可能需要在http请求的
headers
选项中设置它。 Normally something like this : 通常是这样的:
headers: {'Authorization': '[your API key]'}
Here is an example from another Question 以下是另一个问题的示例
$http({method: 'GET', url: '[the-target-url]', headers: {
'Authorization': '[your-api-key]'}
});
Edit : Just saw you wanted to store the response in a variable. 编辑:刚看到你想将响应存储在一个变量中。 In this case I would probably just use AJAX.
在这种情况下,我可能只是使用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 我从这个问题得到了这个,我正在工作,所以我现在无法测试它,但看起来很稳固
Edit 2: Pretty sure you should be able to use this line : 编辑2:很确定你应该能够使用这一行:
headers: {'Authorization': '[your API key]'},
instead of the beforeSend
line in the first edit. 而不是第一次编辑中的
beforeSend
行。 This may be simpler for you 这对你来说可能更简单
With your own Code and a Slight Change withou jQuery, 使用您自己的代码和使用jQuery进行轻微更改 ,
function testingAPI(){
var key = "8a1c6a354c884c658ff29a8636fd7c18";
var url = "https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015";
console.log(httpGet(url,key));
}
function httpGet(url,key){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false );
xmlHttp.setRequestHeader("Ocp-Apim-Subscription-Key",key);
xmlHttp.send(null);
return xmlHttp.responseText;
}
Thank You 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.