Javascript不会按价格/列表类型过滤eBay api结果

时间:2013-12-25 00:27:42

标签: javascript html json api ebay

在遵循eBay API指南显示属于指定价格范围的固定价格商品后,结果仍然显示不同价格范围之外的基于拍卖的商品。我一字不漏地跟着他们的教程,所以我不确定我做错了什么。

代码:

<div id="api"></div>

<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;
    if (null != title && null != viewitem)
    {
      html.push(
        '<tr id="api_microposts"><td>'
         + '<img src="' + pic + '" border="0" width="190">' + '<a href="' + viewitem + '" target="_blank">' + title + 
         '</a></td></tr>');
    }
  }
  html.push('</tbody></table>');
  document.getElementById("api").innerHTML = html.join("");

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


// Define global variable for the URL filter
var urlfilter = "";


// Generates an indexed URL snippet from the array of item filters
function  buildURLArray() {
  // Iterate through each filter in the array
  for(var i=0; i<filterarray.length; i++) {
    //Index each item filter in filterarray
    var itemfilter = filterarray[i];
    // Iterate through each parameter in each item filter
    for(var index in itemfilter) {
      // Check to see if the parameter 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];
        }
      }
    }
  }
}  // End buildURLArray() function

// Execute the function to build the URL filter
buildURLArray(filterarray);

url += urlfilter;



}
</script>


<!--
Use the value of your appid for the appid parameter below.
-->
<script src=http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=*App ID*&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&keywords=iphone%205%2016gb%20unlocked&paginationInput.entriesPerPage=3>
</script>

1 个答案:

答案 0 :(得分:1)

规范来自this page。我将分两步解决这个问题:

即使你注释掉这一行,检查你是否得到相同的结果:

    url += urlfilter;

如果发生这种情况,那么您的问题就是您提出请求的方式,而您设置的参数还不相关。如果确实发生了变化,那么请求就会很顺利,你需要弄清楚你传递的内容。

在这种情况下,参数需要一些摆弄。如果您收到任何结果,那么 ListingType 过滤器可能会出现一个问题。规范说ListType接受一个字符串OR可以取多个值。可能是您想要使用:

    {"name":"ListingType",
    "value": "FixedPrice",
    "paramName":"",
    "paramValue":""}