跨域jQuery的阿贾克斯(JSONP):未捕获的SyntaxError:意外的标记:(冒号) [英] Cross domain jquery ajax (Jsonp): Uncaught SyntaxError: Unexpected token : (colon)

查看:284
本文介绍了跨域jQuery的阿贾克斯(JSONP):未捕获的SyntaxError:意外的标记:(冒号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图从蒸汽API提取数据,以及有没有运气,因为我总是得到上述错误。这里是code我使用的:

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.com/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;
            }
        });
}

我忽略我的API密钥。 我也看了看很多其他职位,并不能找出问题所在。我已经尝试使用JSONP,定期JSON试过,我也尝试过使用&放大器;回调= steamurl ,但无济于事。

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.

推荐答案

对此的解决方案是增加一个本地代理,你的jQuery code将调用。您的代理将服务器端code(PHP,Python和Ruby等)转发到阀门的查询,然后返回到您的jQuery电话。你会,但是,必须使用其支持的格式(其中JSONP是不是一个)。

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:

  • 创建一个接受jQuery将被传递参数PHP文件。在这种情况下,它看起来像帐户ID,与要接收。不要通过API密钥,这应该被保存在PHP文件
  • 在PHP中,建立你的 steamurl 使用存储的API密钥,并且两个传递的值
  • 在发行使用该 steamurl 和检索结果调用阀服务器。
  • 在返回此响应你的Ajax调用
  • 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

您的PHP会是这个样子(也应该包括更多的错误检查,因为我只是服用 $ _ GET 值福音:

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.com/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;

现在,您可以使用jQuery来解析这个JSON响应。

Now you can use jQuery to parse this JSON response.

这篇关于跨域jQuery的阿贾克斯(JSONP):未捕获的SyntaxError:意外的标记:(冒号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆