Uncaught ReferenceError: jsonFlickrFeed is not defined
Uncaught ReferenceError: jsonFlickrFeed is not defined
我有以下要求:
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: { "format": "json" },
success: function (data) {
console.log(data); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
但最后我得到
Uncaught ReferenceError: jsonFlickrFeed is not defined
at photos_public.gne?&callback=jQuery331016421245174669186_1523107884637&format=json&_=1523107884638:1
我做错了什么,我该如何解决?提前致谢!
您的 URLflickrApiPoint
不完整。必须是 const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
完整示例:
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: {format: "json"},
success: function (data) {
console.log(data); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
因为您正在使用 jsonp ajax 调用,flickr 服务 returns 对函数的调用:jsonFlickrFeed
这意味着您必须在代码中自己定义这样的函数:
function jsonFlickrFeed(json) {
console.log(json);
$.each(json.items, function (i, item) {
$("<img />").attr("src", item.media.m).appendTo("#images");
});
}
这样的功能会在 ajax 完成后自动执行。因此,您需要定义一个 jsonFlickrFeed 函数回调,而不是成功 ajax 回调。
function jsonFlickrFeed(json) {
//console.log(json);
console.log('jsonFlickrFeed called');
$.each(json.items, function (i, item) {
$("<img />").attr("src", item.media.m).appendTo("#images");
});
}
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: { "format": "json" },
complete: function (data) {
console.log('ajax call completed'); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="images"></div>
使用参数nojsoncallback=1
只获取JSON个对象。
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1";
我有以下要求:
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: { "format": "json" },
success: function (data) {
console.log(data); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
但最后我得到
Uncaught ReferenceError: jsonFlickrFeed is not defined
at photos_public.gne?&callback=jQuery331016421245174669186_1523107884637&format=json&_=1523107884638:1
我做错了什么,我该如何解决?提前致谢!
您的 URLflickrApiPoint
不完整。必须是 const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
完整示例:
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: {format: "json"},
success: function (data) {
console.log(data); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
因为您正在使用 jsonp ajax 调用,flickr 服务 returns 对函数的调用:jsonFlickrFeed 这意味着您必须在代码中自己定义这样的函数:
function jsonFlickrFeed(json) {
console.log(json);
$.each(json.items, function (i, item) {
$("<img />").attr("src", item.media.m).appendTo("#images");
});
}
这样的功能会在 ajax 完成后自动执行。因此,您需要定义一个 jsonFlickrFeed 函数回调,而不是成功 ajax 回调。
function jsonFlickrFeed(json) {
//console.log(json);
console.log('jsonFlickrFeed called');
$.each(json.items, function (i, item) {
$("<img />").attr("src", item.media.m).appendTo("#images");
});
}
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne";
try {
$.ajax({
url: flickrApiPoint,
dataType: 'jsonp',
data: { "format": "json" },
complete: function (data) {
console.log('ajax call completed'); //formatted JSON data
}
});
}
catch (e) {
console.log(e);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="images"></div>
使用参数nojsoncallback=1
只获取JSON个对象。
const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1";