2013-11-02 43 views
0

我正在使用Yummly API(https://developer.yummly.com/documentation),我試圖解析JSONP列表中的課程以便在下拉框中使用。我請求文件的格式(位於http://api.yummly.com/v1/api/metadata/course?_app_id=[My應用程序ID] & _app_key = [我的APP鍵)是:未捕獲的ReferenceError:未定義set_metadata

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

該請求似乎工作正常,我可以在Chrome的「網絡」選項卡中查看結果。然而,在控制檯中,我收到錯誤「Uncaught 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); 
}); 

任何幫助是極大的讚賞。我也非常感謝我對錯過的解釋,而不僅僅是修復。謝謝。

+0

嘗試'$阿賈克斯({ 網址:coursesURL, 數據類型: 'JSONP', 成功:功能(數據){// } } )' – shakib

回答

0

我想出了這個問題。

JSONP返回的不是JSON文本,而是回調函數。因此,我在我的代碼中需要一個名爲「set_metadata」的函數,該函數在json/ajax調用成功時使用。

具體來說,我定義的函數

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

我測試了它,並且正確地捕捉我試圖獲取數據。

1

我把這個作爲答案加入而不是評論的原因是因爲我沒有足夠的評論聲望,而且這是我在返回jsonp的yummly api上唯一能找到的。

我能夠通過「uncaught 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

+0

你應該問這個,因爲它是自己的問題(如果你願意的話可以鏈接到這個問題)。這樣你就可以開始建立聲望。一旦你這樣做,編輯你的文章與問題的鏈接,我會盡力幫助你! –

相關問題