Javascript 未捕获引用错误:未定义集合\元数据
我正在使用Yummly API()并试图在下拉框中解析要使用的课程的JSONP列表。我请求的文件格式(位于http://api.yummly.com/v1/api/metadata/course?_app_id=[我的应用程序ID]&我的应用程序密钥=[我的应用程序密钥])是: 这个请求似乎工作正常,我可以在Chrome的“网络”选项卡中查看结果。但是,在控制台中,我得到了错误“UncaughtReferenceError:set_metadata is not defined”(未捕获引用错误:未定义元数据)。我已经做了很多环顾四周的工作,并找到了具有类似但不同错误的人,但我还不了解错误的原因或修复工作的原因。我对jQuery相当陌生,所以我猜我的请求有问题,这是:Javascript 未捕获引用错误:未定义集合\元数据,javascript,jquery,jsonp,Javascript,Jquery,Jsonp,我正在使用Yummly API()并试图在下拉框中解析要使用的课程的JSONP列表。我请求的文件格式(位于http://api.yummly.com/v1/api/metadata/course?_app_id=[我的应用程序ID]&我的应用程序密钥=[我的应用程序密钥])是: 这个请求似乎工作正常,我可以在Chrome的“网络”选项卡中查看结果。但是,在控制台中,我得到了错误“UncaughtReferenceError:set_metadata is not defined”(未捕获引用错误
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);
});
非常感谢您的帮助。我也非常希望能解释一下我遗漏了什么,而不仅仅是修复。多谢各位 我解决了这个问题 JSONP返回的不是JSON文本,而是回调函数。因此,我需要在代码中使用一个名为“set_metadata”的函数,该函数在json/ajax调用成功时使用 具体来说,我定义了一个函数
function set_metadata(course, data) {
//Do stuff here
};
我测试了它,它正确地捕获了我试图获取的数据。我之所以添加此作为答案而不是评论,是因为我没有足够的声誉来评论,这是我在返回jsonp的yummly api上唯一能找到的东西 我能够克服“uncaughtreferenceerror”的问题,但现在它只返回了响应中的“过敏”一词,而我没有得到其余的数据 这是我的密码:
$.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调用中,我通过了引用错误,但我没有得到响应中的其余数据
请帮忙?
Finbartry
$.ajax({url:courseurl,数据类型:'jsonp',success:function(data){/})
你应该问这个问题,因为这是你自己的问题(如果你想的话,可以链接到这个问题)。这样你就可以开始建立声誉。一旦你这样做了,编辑你的文章,并链接到问题,我会尽力帮助你!
jsonpCallback:"set_metadata",