未捕获的ReferenceError:未定义set_metadata

时间:2013-11-02 21:05:26

标签: javascript jquery jsonp

我正在使用Yummly API(https://developer.yummly.com/documentation),我正在尝试解析要在下拉框中使用的JSONP课程列表。我请求的文件格式(位于http://api.yummly.com/v1/api/metadata/course?_app_id=[My App ID]& _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);
});

非常感谢任何帮助。我也非常感谢我对失踪的解释,而不仅仅是修复。谢谢。

2 个答案:

答案 0 :(得分:1)

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

我能够超越“未被捕获的referenceError”问题,但现在它只返回“过敏”这个词,这是在响应中,而我没有得到其余的数据。

这是我的代码:

    $.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调用中的

让我超过了引用错误,但我没有得到响应中的其余数据。

请帮帮忙? Finbar

答案 1 :(得分:0)

我发现了问题。

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

具体来说,我定义了函数

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

我测试了它并正确捕获了我想要的数据。