2012-10-31 81 views
0

一直在看EBAY API教程EBAY API:findItemsAdvanced如何通過關鍵字和類別進行搜索?

http://developer.ebay.com/DevZone/finding/HowTo/GettingStarted_JS_NV_JSON/GettingStarted_JS_NV_JSON.html

,但我想知道我可以根據類別和關鍵字,我通過使用操作findItemsAdvanced這樣做搜索,我真的不知道是什麼在這裏做一直試圖使它工作,但我回來是一個空白頁面。這裏的html:

<html> 
<head> 
<title>eBay Search Results</title> 
<style type="text/css">body { font-family: arial,sans-serif;} </style> 
</head> 
<body> 
<h1>eBay Search Results</h1> 
<div id="results"></div> 
<script> 
//Parse the response and build an HTML table to display search results 
function _cb_findItemsAdvanced(root) { 

     var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || []; 
     var html = []; 
     html.push('<table width="100%" border="0" cellspacing="0" cellpadding="3"><tbody>'); 
     for (var i = 0; i < items.length; ++i) { 
     var item  = items[i]; 
     var title = item.title; 
     var pic  = item.galleryURL; 
     var viewitem = item.viewItemURL; 
     var price = item.sellingStatus[0].currentPrice[0].__value__; 

     if (null != title && null != viewitem) { 
      html.push('<tr><td>' + '<img src="' + pic + '" border="0">' + '</td>' + 
      '<td><a href="' + viewitem + '" target="_blank">' + title + '</a></td>'+ '<td>' + price + "$" + '</td></tr>'); 
     } 
     } 
     html.push('</tbody></table>'); 
     document.getElementById("results").innerHTML = html.join(""); 

} // End _cb_findItemsByKeywords() function 
//Construct the request 
//Replace MyAppID with your Production AppID 
var url = "http://svcs.ebay.com/services/search/FindingService/v1"; 
url += "?OPERATION-NAME=findItemsAdvanced"; 
url += "&SERVICE-VERSION=1.0.0"; 
url += "&SECURITY-APPNAME=AppID"; 
url += "&GLOBAL-ID=EBAY-US"; 
url += "&RESPONSE-DATA-FORMAT=JSON"; 
url += "&callback=_cb_findItemsAdvanced"; 
url += "&REST-PAYLOAD"; 
url += "&categoryId=1"; // video game 
url += "&keywords=digimon%20world%201"; // change value to game title 
url += "&paginationInput.entriesPerPage=6"; 

//Submit the request 
s=document.createElement('script'); // create script element 
s.src= url; 
document.body.appendChild(s); 
</script> 

</body> 
</html> 

回答

4

你的代碼中有兩個輕微的錯誤。

第一個是categoryId - 在您的示例中,您指定的是categoryId=1。對於美國eBay網站上的視頻遊戲,這應該是categoryId=139973。這應該會在您的API調用中爲您提供結果。

第二個問題是,當你來到解析上線13 API調用:

var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];

你的代碼是尋找一個findItemsByKeywordsResponse但你的API調用是一個findItemsAdvanced - 所以你需要改變線在你的Javascript功能:

var items = root.findItemsAdvancedResponse[0].searchResult[0].item || [];

我剛剛測試了這一點,它完美的作品!

+0

你是如何獲得視頻遊戲categoryId的?在找不到也無妨,可能是我錯過了從API的東西... –

+1

易趣的categoryId的可以在這裏找到: [鏈接](http://listings.ebay.co.uk/_W0QQloctZShowCatIdsQQsacatZQ2d1QQsalocationZlicQQsocmdZListingCategoryList) (更換.co.uk使用正確的國家/地區域名) 還有一個可供下載的所有catID的電子表格: [link](http://www.cgmlab.com/ebay-category-tree-download/) 其中每週自動更新一次。 – Skillbean