0

我正在使用 Yummly API ( https://developer.yummly.com/documentation ),我正在尝试解析 JSONP 课程列表以在下拉框中使用。我请求的文件格式(位于 http://api.yummly.com/v1/api/metadata/course?_app_id=[My App ID]&_app_key=[My App Key])是:

set_metadata('course', [{"id":"course-Main Dishes","type":"course","description":"Main Dishes","searchValue":"course^course-Main Dishes"},....................}])

该请求似乎工作正常,我可以在 Chrome 的“网络”选项卡中查看结果。但是,在控制台中我收到错误“未捕获的 ReferenceError:set_metadata 未定义”我已经做了很多环顾四周,发现人们有类似但不同的错误,但我不明白原因或修复的原因他们的错误有效。我对 jQuery 相当陌生,所以我猜我的请求有问题,即:

var coursesURL = 'http://api.yummly.com/v1/api/metadata/course?_app_id=' + appID + '&_app_key=' + appKey;
var courses = [];

//Query for the list
$.getJSON(coursesURL + '?callback=?', null, function(data) {
    console.log(data);
    //Go through each result object found
    $.each(data.course, function(i, course) {
        courses.push(course.description);
    });
    console.log(courses);
});

任何帮助是极大的赞赏。我也非常感谢对我所缺少的东西的解释,而不仅仅是修复。谢谢你。

4

2 回答 2

1

我将其添加为答案而不是评论的原因是因为我没有足够的声誉来发表评论,这是我在返回 jsonp 的 yummly api 上唯一能找到的东西。

我能够克服“未捕获的引用错误”问题,但现在它只返回响应中的“过敏”一词,而我没有得到其余的数据。

这是我的代码:

    $.ajax({
        url:"//api.yummly.com/v1/api/metadata/allergy?_app_id=[APP_ID]&_app_key=[APP_KEY]?callback=",
        dataType:"jsonp",
        jsonpCallback:"set_metadata",
        beforeSend:function(){
            console.log("sending");
        },
        success: function (data){
            console.log(data);
        },
        error: function(data){
            console.log("send error and returned:");
            console.log(data);
        }
    });

这是回应:

set_metadata('allergy', [

    {"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]},

    {"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US","en-GB"]}

]);

代码行说:

jsonpCallback:"set_metadata",

在 ajax 调用中让我超越了参考错误,但我没有得到响应中的其余数据。

请帮忙?鳍条

于 2014-04-13T17:20:15.623 回答
0

我解决了这个问题。

JSONP 返回的不是 JSON 文本,而是回调函数。因此,我的代码中需要一个名为“set_metadata”的函数,该函数在 json/ajax 调用成功时使用。

具体来说,我定义了函数

function set_metadata(course, data) {
    //Do stuff here
};

我对其进行了测试,它正确地捕获了我想要获取的数据。

于 2013-11-03T19:11:26.703 回答