Uncaught ReferenceError: jsonFlickrFeed is not defined

蓝咒 提交于 2019-12-11 05:37:00

问题


I have the following request :

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);
}

but in the end I am getting

Uncaught ReferenceError: jsonFlickrFeed is not defined
    at photos_public.gne?&callback=jQuery331016421245174669186_1523107884637&format=json&_=1523107884638:1

What I am not doing right and how can I fix it? Thanks in advance!


回答1:


Your URLflickrApiPoint is incomplete. It has to be const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

Full example:

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);
    }



回答2:


Because you are using a jsonp ajax call, the flickr services returns a call to the function: jsonFlickrFeed That means you must define by yourself in your code such a function like:

function jsonFlickrFeed(json) {
    console.log(json);

    $.each(json.items, function (i, item) {
        $("<img />").attr("src", item.media.m).appendTo("#images");
    });
}

Such a function is executed automatically on ajax done. Hence, instead of the success ajax callback you need to define a jsonFlickrFeed function callback.

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>



回答3:


Use the parameter nojsoncallback=1 to get only JSON object.

const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1";


来源:https://stackoverflow.com/questions/49707931/uncaught-referenceerror-jsonflickrfeed-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!