admin管理员组文章数量:1024716
I've been trying to pull data from the steam api, and have had no luck because I always get the above error. Here is the code I am using:
var steamurl = "/?key=[keyomitted]&account_id=38440257&Matches_Requested=10";
function populate_api(){
var json;
$.ajax({
'url': steamurl,
'dataType': "jsonp",
'success': function (data) {
alert('success');
json = data;
}
});
}
I omitted my API key.
I have looked at many other posts, and cannot figure out where the problem is. I have tried using Jsonp, regular json, I have also tried using "&callback=?"
after steamurl
, but to no avail.
I've been trying to pull data from the steam api, and have had no luck because I always get the above error. Here is the code I am using:
var steamurl = "https://api.steampowered./IDOTA2Match_570/GetMatchHistory/V001/?key=[keyomitted]&account_id=38440257&Matches_Requested=10";
function populate_api(){
var json;
$.ajax({
'url': steamurl,
'dataType': "jsonp",
'success': function (data) {
alert('success');
json = data;
}
});
}
I omitted my API key.
I have looked at many other posts, and cannot figure out where the problem is. I have tried using Jsonp, regular json, I have also tried using "&callback=?"
after steamurl
, but to no avail.
- can you share the response format from the sever – Arun P Johny Commented Feb 11, 2014 at 12:24
- I don't think that API supports JSONP, only JSON. – Barmar Commented Feb 11, 2014 at 12:24
- See developer.valvesoftware./wiki/Steam_Web_API/… – Arun P Johny Commented Feb 11, 2014 at 12:25
- @Barmar I have also tried JSON as the datatype with and without the callback and it did not work. – k4kuz0 Commented Feb 11, 2014 at 12:25
- @ArunPJohny forgive my inexperience, but how do I find the response from the server? I do not feel as though I am getting one. – k4kuz0 Commented Feb 11, 2014 at 12:26
1 Answer
Reset to default 6The solution for this is to add a local proxy that your jQuery code will call. Your proxy will be server side code (PHP, Python, Ruby, etc) that forwards the query on to Valve and then returns it to your jQuery call. You will, however, have to use one of their supported formats (of which JSONP is not one).
A high level view of what you'll be doing:
- Create PHP file that accepts the parameters jQuery will be passing. In this case, it looks like account ID and matches you want to receive. Do not pass the API key, this should be stored in the PHP file
- In PHP, build your
steamurl
using the stored API key, and the two passed values - Issue a call to the Valve servers using this
steamurl
and retrieve the results. - Return this response to your ajax call
Your PHP will look something like this (and should include more error checking since I am just taking the $_GET
values as gospel:
$matches = $_GET['matches'];
$acct = $_GET['accountid'];
$APIKEY = <YOURKEYHERE>;
$steamurl = "https://api.steampowered./IDOTA2Match_570/GetMatchHistory/V001/?key=$APIKEY&account_id=$acct&Matches_Requested=$matches&format=json";
$json_object= file_get_contents($steamurl);
header('Content-Type: application/json');
echo $json_object;
Now you can use jQuery to parse this JSON response.
I've been trying to pull data from the steam api, and have had no luck because I always get the above error. Here is the code I am using:
var steamurl = "/?key=[keyomitted]&account_id=38440257&Matches_Requested=10";
function populate_api(){
var json;
$.ajax({
'url': steamurl,
'dataType': "jsonp",
'success': function (data) {
alert('success');
json = data;
}
});
}
I omitted my API key.
I have looked at many other posts, and cannot figure out where the problem is. I have tried using Jsonp, regular json, I have also tried using "&callback=?"
after steamurl
, but to no avail.
I've been trying to pull data from the steam api, and have had no luck because I always get the above error. Here is the code I am using:
var steamurl = "https://api.steampowered./IDOTA2Match_570/GetMatchHistory/V001/?key=[keyomitted]&account_id=38440257&Matches_Requested=10";
function populate_api(){
var json;
$.ajax({
'url': steamurl,
'dataType': "jsonp",
'success': function (data) {
alert('success');
json = data;
}
});
}
I omitted my API key.
I have looked at many other posts, and cannot figure out where the problem is. I have tried using Jsonp, regular json, I have also tried using "&callback=?"
after steamurl
, but to no avail.
- can you share the response format from the sever – Arun P Johny Commented Feb 11, 2014 at 12:24
- I don't think that API supports JSONP, only JSON. – Barmar Commented Feb 11, 2014 at 12:24
- See developer.valvesoftware./wiki/Steam_Web_API/… – Arun P Johny Commented Feb 11, 2014 at 12:25
- @Barmar I have also tried JSON as the datatype with and without the callback and it did not work. – k4kuz0 Commented Feb 11, 2014 at 12:25
- @ArunPJohny forgive my inexperience, but how do I find the response from the server? I do not feel as though I am getting one. – k4kuz0 Commented Feb 11, 2014 at 12:26
1 Answer
Reset to default 6The solution for this is to add a local proxy that your jQuery code will call. Your proxy will be server side code (PHP, Python, Ruby, etc) that forwards the query on to Valve and then returns it to your jQuery call. You will, however, have to use one of their supported formats (of which JSONP is not one).
A high level view of what you'll be doing:
- Create PHP file that accepts the parameters jQuery will be passing. In this case, it looks like account ID and matches you want to receive. Do not pass the API key, this should be stored in the PHP file
- In PHP, build your
steamurl
using the stored API key, and the two passed values - Issue a call to the Valve servers using this
steamurl
and retrieve the results. - Return this response to your ajax call
Your PHP will look something like this (and should include more error checking since I am just taking the $_GET
values as gospel:
$matches = $_GET['matches'];
$acct = $_GET['accountid'];
$APIKEY = <YOURKEYHERE>;
$steamurl = "https://api.steampowered./IDOTA2Match_570/GetMatchHistory/V001/?key=$APIKEY&account_id=$acct&Matches_Requested=$matches&format=json";
$json_object= file_get_contents($steamurl);
header('Content-Type: application/json');
echo $json_object;
Now you can use jQuery to parse this JSON response.
版权声明:本文标题:javascript - Cross domain jquery ajax (Jsonp): Uncaught SyntaxError: Unexpected token : (colon) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619695a2159499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论