什么将数据归类为 JSONP,我如何使用普通 javascript 获取它?
What classifies data as JSONP and how can I get it with plain javascript?
所以我最近学会了如何进行 AJAX 调用,并且知道在使用来自外部服务器的 APIs 时我需要使用 JSONP。正如我从 sitepoint 了解到的,有人指出 JSONP 的不同之处在于它被包装在一个函数中,使其可以通过脚本标签访问。
站点点Link:https://www.sitepoint.com/jsonp-examples/
例如 http://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json
虽然我不明白的是 JSONP 的 Flickr API。
Flickr API Link JSONP:
https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?
Flickr API Link XML:
https://api.flickr.com/services/feeds/photos_public.gne?
好像是用returnXML数据代替的。更让我困惑的是,这种类型的 xml 被 jQuery 的 $.getJSON() 接受,而它是原始的 api 也在 xml 中导致同源策略错误。我发现它们之间的主要区别是一些 href 更改。
那么是什么造就了 flickr JSONP 的 XML 以及我如何用它进行 ajax 调用 javascript?
如有任何反馈,我们将不胜感激:)
为清楚起见更新:
Here I have working code using the flickr XML that does an ajax call with jQuery.
Link: https://jsfiddle.net/Jonathan002/05ao4d87/
有人告诉我,只有树屋中的 JSONP 才能做到这一点。 https://teamtreehouse.com/library/ajax-basics/ajax-and-apis/displaying-the-photos。如果我不使用 JSONP 来完成此任务,是什么让我可以规避同源策略?
你说的 link returns JSONP 没有,它 returns XML。 flickr documentation tells us you need format=json
in the URL; you also want to fill in something where the ?
is so you name the callback function (although if you use jQuery, it will do that for you). For instance: https://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=callbackName
注意:他们称之为 JSON,但事实并非如此。是 JSONP。 JSON and JSONP 是不同的东西。回调调用中的位是有效的 JSON,但(并非所有 JSONP 响应都是有效的)。
这里有一个 JSON 的例子:
{"foo": "bar"}
下面是 JSONP 在回调中使用有效 JSON 的示例:
callbackName({"foo": "bar"})
下面是 JSONP 在回调中使用无效 JSON 的示例(但它有效,因为 JSONP 和 JSON 是不同的东西):
callbackName({foo: "bar"})
how can I get it with plain javascript?
jQuery documentation for $.ajax
描述了如何用 jQuery 进行 JSON 调用。在这种情况下:
$.ajax({
url: "https://api.flickr.com/services/feeds/photos_public.gne?format=json",
dataType: "jsonp",
jsonp: "jsoncallback",
success: function(data) {
// Use the data here
console.log("The title is: " + data.title);
},
error: function() {
// Handle error here
console.log("Error loading the data");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
请注意,由于 flickr 使用 non-standard 名称作为控制回调名称的参数,您需要 jsonp
选项告诉 jQuery 要使用的参数名称。
所以我最近学会了如何进行 AJAX 调用,并且知道在使用来自外部服务器的 APIs 时我需要使用 JSONP。正如我从 sitepoint 了解到的,有人指出 JSONP 的不同之处在于它被包装在一个函数中,使其可以通过脚本标签访问。
站点点Link:https://www.sitepoint.com/jsonp-examples/ 例如 http://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json
虽然我不明白的是 JSONP 的 Flickr API。
Flickr API Link JSONP: https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=? Flickr API Link XML: https://api.flickr.com/services/feeds/photos_public.gne?
好像是用returnXML数据代替的。更让我困惑的是,这种类型的 xml 被 jQuery 的 $.getJSON() 接受,而它是原始的 api 也在 xml 中导致同源策略错误。我发现它们之间的主要区别是一些 href 更改。
那么是什么造就了 flickr JSONP 的 XML 以及我如何用它进行 ajax 调用 javascript?
如有任何反馈,我们将不胜感激:)
为清楚起见更新:
Here I have working code using the flickr XML that does an ajax call with jQuery.
Link: https://jsfiddle.net/Jonathan002/05ao4d87/
有人告诉我,只有树屋中的 JSONP 才能做到这一点。 https://teamtreehouse.com/library/ajax-basics/ajax-and-apis/displaying-the-photos。如果我不使用 JSONP 来完成此任务,是什么让我可以规避同源策略?
你说的 link returns JSONP 没有,它 returns XML。 flickr documentation tells us you need format=json
in the URL; you also want to fill in something where the ?
is so you name the callback function (although if you use jQuery, it will do that for you). For instance: https://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=callbackName
注意:他们称之为 JSON,但事实并非如此。是 JSONP。 JSON and JSONP 是不同的东西。回调调用中的位是有效的 JSON,但(并非所有 JSONP 响应都是有效的)。
这里有一个 JSON 的例子:
{"foo": "bar"}
下面是 JSONP 在回调中使用有效 JSON 的示例:
callbackName({"foo": "bar"})
下面是 JSONP 在回调中使用无效 JSON 的示例(但它有效,因为 JSONP 和 JSON 是不同的东西):
callbackName({foo: "bar"})
how can I get it with plain javascript?
jQuery documentation for $.ajax
描述了如何用 jQuery 进行 JSON 调用。在这种情况下:
$.ajax({
url: "https://api.flickr.com/services/feeds/photos_public.gne?format=json",
dataType: "jsonp",
jsonp: "jsoncallback",
success: function(data) {
// Use the data here
console.log("The title is: " + data.title);
},
error: function() {
// Handle error here
console.log("Error loading the data");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
请注意,由于 flickr 使用 non-standard 名称作为控制回调名称的参数,您需要 jsonp
选项告诉 jQuery 要使用的参数名称。