简体   繁体   English

如何使用jsonp请求获取json

[英]How to get json using a jsonp request

I'm trying to use instagrams api, without having to authenticate. 我正在尝试使用instagrams api,而无需进行身份验证。 if I go to: https://www.instagram.com/explore/tags/chicago/?__a=1 如果我去: https : //www.instagram.com/explore/tags/chicago/?__a=1

I get a json response. 我收到一个json响应。 I'm trying to build an angular 4 provider that returns that data. 我正在尝试构建一个返回该数据的angular 4提供程序。

getInsta(term){
    return this.jsonp.request(this.url+term+'/?__a=1&callback=__ng_jsonp__.__req0.finished')
      .map(res => {
        console.log(res.json());
      });
  }

I've tried "callback" and "c". 我已经尝试过“回调”和“ c”。 I've tried JSONP_CALLBACK. 我已经尝试过JSONP_CALLBACK。

I get the error of: 我收到以下错误:

"SyntaxError: Unexpected token ':'. Parse error. - In chicago:1" “ SyntaxError:意外令牌':'。解析错误。-在芝加哥:1”

and

"JSONP injected script did not invoke callback" “注入JSONP的脚本未调用回调”

If I click on the unexpected token error, it brings me to the response with all the json data in it. 如果单击意外的令牌错误,它将带我所有的json数据进入响应。 So that means it's coming through, I just cant access it! 因此,这意味着它正在通过,我无法访问它!

Any idea how i can get around this? 知道我如何解决这个问题吗?

Here is a screenshot of the error and when clicking on the error 这是错误以及在点击错误时的屏幕截图 在此处输入图片说明

在此处输入图片说明

JSONP is a method to fetch data cross origin, it works because it injects a script tag to the body when the src of the file contains the callback function name. JSONP是一种获取跨源数据的方法,它之所以起作用,是因为当文件的src包含callback函数名称时,它会向主体中注入script标记。

<script src="http://ani.site.com/api/?callback=myGlobalFunc">

And it assumes that there is a global func on the window with the name myGlobalFunc . 并且假定窗口上有一个名为myGlobalFunc的全局函数。

The response of that call gonna look like: myGlobalFunc({data: 'data1'}) . 该调用的响应将类似于: myGlobalFunc({data: 'data1'}) Invocation of the function with one argument which is the data. 使用一个参数即数据来调用该函数。

In your case, Instagram API return JSON not JSONP . 在您的情况下,Instagram API返回JSON而不是JSONP

There for you can't use that mechanism. 在那里,您不能使用该机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM