最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Cross domain jquery ajax (Jsonp): Uncaught SyntaxError: Unexpected token : (colon) - Stack Overflow

matteradmin4PV0评论

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.

Share Improve this question asked Feb 11, 2014 at 12:22 k4kuz0k4kuz0 1,0451 gold badge11 silver badges24 bronze badges 10
  • 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
 |  Show 5 more ments

1 Answer 1

Reset to default 6

The 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.

Post a comment

comment list (0)

  1. No comments so far