2014-04-03 61 views
0

我正在嘗試構建一個簡單的網站,用於檢查並打印出現在購買汽車的價格。我無法使用javascript「push」函數來打印除字符串之外的任何內容。 eBay API表示,buyItNowPrice返回金額,如下所示:http://developer.ebay.com/DevZone/merchandising/docs/CallRef/types/Item.html#buyItNowPrice。我已經試驗過其他項目(http://developer.ebay.com/DevZone/merchandising/docs/CallRef/types/Item.html)的功能,唯一對我有用的是返回字符串的項目。eBay API - 無法打印buyItNowPrice

的問題是,應該如何

var itemPrice = item.buyItNowPrice; 

行被格式化輸出是多少?

代碼:

<script> 
    function _cb_findItemsByKeywords(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 itemPrice = item.buyItNowPrice; 
    var timeLeft = item.watchCount; 
    if (title != null && null != viewitem) { 
     html.push('<tr><td>' + '<img src="' + pic + '" border="1">' + '</td>' 
     + '<td><a href="' + viewitem + '" target="_blank">' 
     + title + '</a>' // end hyperlink 
     + '<br>Item Price: ' + itemPrice 
     + '<br>Time Remaining: ' + timeLeft 
     + '</td></tr>');} 
    } 
    html.push('</tbody></table>'); 
    document.getElementById("results").innerHTML = html.join(""); 
} 

// Create a JavaScript array of the item filters you want to use in your request 
var filterarray = [ 
    {"name":"MaxPrice", "value":"250000", "paramName":"Currency", "paramValue":"USD"}, 
    {"name":"MinPrice", "value":"15000", "paramName":"Currency", "paramValue":"USD"}, 
    //{"name":"FreeShippingOnly", "value":"false", "paramName":"", "paramValue":""}, 
    {"name":"ListingType", "value":["AuctionWithBIN", "FixedPrice", /*"StoreInventory"*/], "paramName":"", "paramValue":""}, 
    ]; 

// Generates an indexed URL snippet from the array of item filters 
var urlfilter = ""; 
function buildURLArray() { 
    for(var i = 0; i < filterarray.length; i++) { 
    var itemfilter = filterarray[i]; 
    for(var index in itemfilter) { 
     // Check to see if the paramter has a value (some don't) 
     if (itemfilter[index] !== "") { 
     if (itemfilter[index] instanceof Array) { 
      for(var r = 0; r < itemfilter[index].length; r++) { 
      var value = itemfilter[index][r]; 
      urlfilter += "&itemFilter\(" + i + "\)." + index + "\(" + r + "\)=" + value ; 
      } 
     } 
     else { 
      urlfilter += "&itemFilter\(" + i + "\)." + index + "=" + itemfilter[index]; 
     } 
     } 
    } 
    } 
} 
buildURLArray(filterarray); 

// Construct the request 
var url = "http://svcs.ebay.com/services/search/FindingService/v1"; 
    url += "?OPERATION-NAME=findItemsByKeywords"; 
    url += "&SERVICE-VERSION=1.0.0"; 
    url += "&SECURITY-APPNAME=REDACTED"; 
    url += "&GLOBAL-ID=EBAY-MOTOR"; 
    url += "&RESPONSE-DATA-FORMAT=JSON"; 
    url += "&callback=_cb_findItemsByKeywords"; 
    url += "&REST-PAYLOAD"; 
    //url += "&categoryId=6001"; 
    url += "&keywords=Ferrari 575"; 
    url += "&paginationInput.entriesPerPage=12"; 
    url += urlfilter; 

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

+0

您不應該隱式聲明itemPrice。我不確定爲什麼這不會如果其他人工作。該物品是否可能不是「立即購買」? – Yuval

+0

我正在查看的所有商品都是現在購買的,如此行所示 {「name」:「ListingType」,「value」:[「AuctionWithBIN」,「FixedPrice」, – robertjskelton

+0

http://developer.ebay。 com/DevZone/merchandising/docs/CallRef/fieldindex.html#buyItNowPrice根據他們的文檔現在購買它只適用於:getMostWatchedItems.itemRecommendations.item, getRelatedCategoryItems.itemRecommendations.item, getSimilarItems.itemRecommendations.item。你在使用其中之一嗎? – Yuval

回答

0

你讀錯eBay的文檔。 FindItemsByKeywords是Finding API service的一部分。在item.listingInfo字段中找到buyItNowPrice字段。將代碼更改爲以下內容將輸出價格。

var itemPrice = '---'; 
// buyItNowPrice may not be returned for all results. 
if(item.listingInfo[0].buyItNowPrice) { 
    itemPrice = item.listingInfo[0].buyItNowPrice[0]['@currencyId'] + ' ' + item.listingInfo[0].buyItNowPrice[0].__value__; 
} else if(item.sellingStatus[0].currentPrice) { 
    itemPrice = item.sellingStatus[0].currentPrice[0]['@currencyId'] + ' ' + item.sellingStatus[0].currentPrice[0].__value__; 
} 
+0

明白了,謝謝! – robertjskelton