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:
我对javascript和web编程很陌生,我需要一些帮助。我有一个HTTP请求,需要通过javascript发送,并需要将输出存储在一个变量中。我试着只用调用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. 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.
但它会返回一个认证错误,因为我没有发送API密钥,它也没有告诉我如何在URL中执行。API键被列成一个头,而不是一个参数,我不知道该怎么做。我尝试使用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.
我只需要弄清楚如何将请求连同密钥一起发送,以及如何将它返回的JSON文档存储为javascript中的变量。
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!
谢谢你!
2 个解决方案
#1
5
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 :
如果它说API键被列为头文件,那么您很可能需要在http请求的头选项中设置它。通常是这样的:
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. Something like this :
编辑:刚才看到您想将响应存储在一个变量中。在这种情况下,我可能只使用AJAX。是这样的:
$.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. This may be simpler for you
而不是第一次编辑的前一行。这对你来说可能更简单
#2
1
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
谢谢你!
#1
5
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 :
如果它说API键被列为头文件,那么您很可能需要在http请求的头选项中设置它。通常是这样的:
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. Something like this :
编辑:刚才看到您想将响应存储在一个变量中。在这种情况下,我可能只使用AJAX。是这样的:
$.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. This may be simpler for you
而不是第一次编辑的前一行。这对你来说可能更简单
#2
1
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
谢谢你!