jQuery的:+的getJSON API SunlightLabs请求帮助 [英] jQuery: getJSON + SunlightLabs API help requested

查看:142
本文介绍了jQuery的:+的getJSON API SunlightLabs请求帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从使用jQuery的函数的getJSON API调用拉着一个特定的元素。我试图用SunlightLab的国会API 拉约立法者的具体信息。在下面的例子中,我试图拉立法者的网站:

I'm having trouble pulling a specific element from an API call using jQuery's getJSON function. I'm trying to use SunlightLab's congress API to pull specific info about legislators. In the example below I'm trying to pull a legislator's website:

$.getJSON("http://services.sunlightlabs.com/api/legislators.get.json?apikey=[api key]&lastname=Weiner&jsonp=?" ,  function(data) {

    alert("hello from sunlight");
    alert(data.response.legislator.website);

}); 

使用上述code,第一个警报显示出来,但竟然不出现第二个警告。我的理解是应该的getJSON在这种情况下要使用JSONP,我认为我有正确设置,结束我的URL'&放大器; JSONP ='。

Using the above code, the first alert shows up but the second alert does not even occur. I understand that getJSON should be using JSONP in this instance, and I think I have that set up correctly, ending my URL with '&jsonp=?'.

把URL在我的getJSON功能在Web浏览器给了我这样的:

Putting the URL in my getJSON function into a web browser gives me this:

({回应:{立委:
  {网站:
  http://weiner.house.gov/,传真:
  202-226-7253,...等。

?({"response": {"legislator": {"website": "http://weiner.house.gov/", "fax": "202-226-7253", ... etc.

我有点被抛出的?示出了在这个开始的时候,但如果第一警报显示出来,然后该请求必须接替...

I'm a little thrown by the '?' showing up at the beginning of this, but if the first alert is showing up then the request must be succeeding...

推荐答案

问号是,因为你所指定的JSONP回调函数是什么?在您的查询字符串(即&放大器;?JSONP = )。由于安全问题(特别是同源策略)你不能做一个AJAX请求到站点同一个域中,你正在浏览的网页之外。为了解决这个问题,JSONP工作方式是创建一个脚本标签,设置在其他网站上的脚本的URL的SRC。这将加载外部JavaScript文件并运行任何code是存在的。现在,为了给外部code用JavaScript的链接,外部API需要一个函数的名字来称呼(&安培; JSONP = functionnametocall )。返回的JavaScript调用该函数并传递它试图返回一个JSON对象作为第一个参数的数据。

The question mark is there because you specified the JSONP callback function to be ? in your query string (ie. &jsonp=?). Due to security concerns (specifically the same-origin policy) you cannot do an AJAX request to a site outside the same domain as the page you're on. To get around this, JSONP works by creating a script tag, with the SRC set to the URL of the script on the other site. This will load the external JavaScript file and run whatever code is there. Now, in order to link that external code with your JavaScript, the external API takes the name of a function to call (&jsonp=functionnametocall). The returned JavaScript calls that function and passes in the data it's trying to return as a JSON object as the first argument.

所以,你看问号,当你去那里是因为你传递一个问号的JSONP查询字符串参数的原因。 jQuery将自动转换问号的URL,例如 http://www.test.com/api/apikey=292929&callback=?来唯一命名的功能。这是在的jQuery后台处理,所以你不必去想它。

So, the reason you see the question mark when you go there is because you're passing a question mark to the jsonp query string parameter. jQuery will automatically convert the question mark in a url such as http://www.test.com/api/apikey=292929&callback=? to a uniquely named function. This is handled in the background by jQuery so you don't have to think about it.

现在,这么说,我不知道是否jQuery的检测,如果回调参数为大于的回调以外的东西的名称=?。然而 $的getJSON()是一个短表的时间越长:

Now, that said, I don't know if jQuery detects if the name of the callback parameter as being something other than callback=?. $.getJSON() however is a short form for the longer:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

我建议您尝试使用 $。阿贾克斯()直接设置 JSONP 设置等于JSONP。这告诉 $。阿贾克斯()方法查询字符串参数被称为 JSONP ,而不是回调。所以像这样基本上是:

I suggest you try using $.ajax() directly and set the jsonp setting equal to "jsonp". This tells the $.ajax() method that the query string parameter is called jsonp and not callback. So like this essentially:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  jsonp:"jsonp"
});

更多信息: $ .getJSON | $。阿贾克斯()

这篇关于jQuery的:+的getJSON API SunlightLabs请求帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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