2012-12-30 66 views
0

使用ProductWiki API搜索產品時,我遇到了JSON解析錯誤。如果我錯過了某些東西,或者返回的數據格式不正確,您可以告訴我嗎?getJSON中的jQuery解析錯誤

這裏是我使用

var searchUrl = "http://api.productwiki.com/connect/api.aspx?" + 
      "&op=search" + 
      "&key=" + API_KEY_PRODUCT_WIKI + 
      "&q=" + nugget + 
      "&format=json" + 
      "&jsoncallback=?"; 

$.getJSON(searchUrl, function (data) { alert("Got data"); }); 

在執行代碼的JavaScript代碼,我從productwiki

{"__type":"ProductWiki.DataStructures.API.Schema.pw_api_results, ProductWiki.DataStructures, Version=1.0.4622.22636, Culture=neutral, PublicKeyToken=null","message":null,"num_results":2,"products":[{"__type":"ProductWiki.DataStructures.API.Schema.product, ProductWiki.DataStructures, Version=1.0.4622.22636, Culture=neutral, PublicKeyToken=null","url":"http://www.productwiki.com/microsoft-surface/","id":15113,"title":"Microsoft Surface","description":null,"proscore":"41","number_of_reviews":"13","category":"Computers","key_features":[],"images":[{"__type":"ProductWiki.DataStructures.API.Schema.image, ProductWiki.DataStructures, Version=1.0.4622.22636, Culture=neutral, PublicKeyToken=null","title":"Microsoft Surface","rawimage":"http://images.productwiki.com/upload/images/microsoft_surface.jpg","largeimage":"http://images.productwiki.com/upload/images/microsoft_surface-400-400.jpg","mediumimage":"http://images.productwiki.com/upload/images/microsoft_surface-100-100.jpg","smallimage":"http://images.productwiki.com/upload/images/microsoft_surface-60-60.jpg"}],"community_review":null,"reviews":null,"tags":null,"competitors":null,"related":null,"skus":null},{"__type":"ProductWiki.DataStructures.API.Schema.product, ProductWiki.DataStructures, Version=1.0.4622.22636, Culture=neutral, PublicKeyToken=null","url":"http://www.productwiki.com/menotek-flexible-bluetooth-waterproof-mini-keyboard/","id":59084,"title":"Menotek Flexible Bluetooth Waterproof Mini Keyboard","description":null,"proscore":"62","number_of_reviews":"1","category":"Keyboards","key_features":[],"images":[{"__type":"ProductWiki.DataStructures.API.Schema.image, ProductWiki.DataStructures, Version=1.0.4622.22636, Culture=neutral, PublicKeyToken=null","title":"Menotek Flexible Bluetooth Waterproof Mini Keyboard","rawimage":"http://images.productwiki.com/upload/images/menotek_flexible_bluetooth_waterproof_mini_keyboard.jpg","largeimage":"http://images.productwiki.com/upload/images/menotek_flexible_bluetooth_waterproof_mini_keyboard-400-400.jpg","mediumimage":"http://images.productwiki.com/upload/images/menotek_flexible_bluetooth_waterproof_mini_keyboard-100-100.jpg","smallimage":"http://images.productwiki.com/upload/images/menotek_flexible_bluetooth_waterproof_mini_keyboard-60-60.jpg"}],"community_review":null,"reviews":null,"tags":null,"competitors":null,"related":null,"skus":null}]} 

得到以下數據傳回,但它也引發了jQuery錯誤

JavaScript critical error at line 1, column 10 in http://api.productwiki.com/connect/api.aspx?&op=search&key=***&q=microsoftsurface&format=json&jsoncallback=jQuery18308245886188083906_1356907329390&_=1356907329577 

SCRIPT1004: Expected ';' 

我嘗試驗證來自productwiki的回覆JSON Formatter & validator並沒有發現任何問題。想知道我是否缺少某些東西,或者產品wiki中的數據不是正確的格式?

+2

jQuery期望JSONP,但響應是JSON。 JSON本身並不是有效的JavaScript,因此你會得到錯誤。服務器必須支持JSONP,所以也許可以試試'format = jsonp'。 –

回答

0

我認爲productwiki不支持jsonp。我結束了從服務器端訪問他們的API