eBay API 使用关键字查找商品 Javascript returns 'Uncaught TypeError'
eBay API find items by keyword using Javascript returns 'Uncaught TypeError'
我是一名初级程序员,我正在尝试使用他们的 Finding API 到 Javascript.
从 eBay 中使用特定关键字为我的混合移动应用程序查找项目
我关注了 this really clear guide,但我的结果中没有显示任何内容 div
。我也试过 运行 网站上的确切代码,但仍然没有结果。我也在现场获得并输入了正确的产品appID
。
代码如下:
<script>
var url = "http://svcs.ebay.com/services/search/FindingService/v1";
url += "?OPERATION-NAME=findItemsByKeywords";
url += "&SERVICE-VERSION=1.0.0";
url += "&SECURITY-APPNAME=sample-sample-PRD-89a6113e8-f7a52044";
url += "&GLOBAL-ID=EBAY-US";
url += "&RESPONSE-DATA-FORMAT=JSON";
url += "&callback=_cb_findItemsByKeywords";
url += "&REST-PAYLOAD";
url += "&keywords=harry%20potter";
url += "&paginationInput.entriesPerPage=3";
url += urlfilter;
alert(url);
// Submit the request
s = document.createElement('script'); // create script element
s.src = url;
// Error received on the line below, "uncaught typeerror: cannot call method 'appendChild' of null"
document.body.appendChild(s);
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><td>' + '<img src="' + pic + '" border="0">' + '</td>' +
'<td><a href="' + viewitem + '" target="_blank">' + title + '</a></td></tr>');
}
}
html.push('</tbody></table>');
document.getElementById("results").innerHTML = html.join("");
} // End _cb_findItemsByKeywords() function
// Create a JavaScript array of the item filters you want to use in your request
var filterarray = [{
"name": "MaxPrice",
"value": "25",
"paramName": "Currency",
"paramValue": "USD"
},
{
"name": "FreeShippingOnly",
"value": "true",
"paramName": "",
"paramValue": ""
},
{
"name": "ListingType",
"value": ["AuctionWithBIN", "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() {
alert("buildURLArray working");
// 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 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];
}
}
}
}
} // End buildURLArray() function
// Execute the function to build the URL filter
buildURLArray(filterarray);
</script>
HTML:
<h1>eBay Search Results</h1>
<div id="results"></div>
有两个警报:
alert(url)
返回了所有内容,除了 urlfilter
是 undefined
alert("buildURLArray working");
没有出现
Eclipse 为第 document.body.appendChild(s);
行返回错误,提示它为空。
Uncaught TypeError: cannot call method 'appendChild' of null
大部分代码来自教程,我不知道为什么我没有得到相同的结果。
在URL构造代码中,由于解释器的顺序性质,没有定义urlFilter;分配没有被提升,即简单地说,你不能在现在使用它的未来价值。所以只需剪切从 var URL= ...(第一行)到 document.body.appendChild(s) 的代码;并将其粘贴到脚本的末尾(在 buildURLArray(filterarray) 之后)tag.Now urlFilter 将获得一些值,之后将用于 URL 构造。
我是一名初级程序员,我正在尝试使用他们的 Finding API 到 Javascript.
从 eBay 中使用特定关键字为我的混合移动应用程序查找项目我关注了 this really clear guide,但我的结果中没有显示任何内容 div
。我也试过 运行 网站上的确切代码,但仍然没有结果。我也在现场获得并输入了正确的产品appID
。
代码如下:
<script>
var url = "http://svcs.ebay.com/services/search/FindingService/v1";
url += "?OPERATION-NAME=findItemsByKeywords";
url += "&SERVICE-VERSION=1.0.0";
url += "&SECURITY-APPNAME=sample-sample-PRD-89a6113e8-f7a52044";
url += "&GLOBAL-ID=EBAY-US";
url += "&RESPONSE-DATA-FORMAT=JSON";
url += "&callback=_cb_findItemsByKeywords";
url += "&REST-PAYLOAD";
url += "&keywords=harry%20potter";
url += "&paginationInput.entriesPerPage=3";
url += urlfilter;
alert(url);
// Submit the request
s = document.createElement('script'); // create script element
s.src = url;
// Error received on the line below, "uncaught typeerror: cannot call method 'appendChild' of null"
document.body.appendChild(s);
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><td>' + '<img src="' + pic + '" border="0">' + '</td>' +
'<td><a href="' + viewitem + '" target="_blank">' + title + '</a></td></tr>');
}
}
html.push('</tbody></table>');
document.getElementById("results").innerHTML = html.join("");
} // End _cb_findItemsByKeywords() function
// Create a JavaScript array of the item filters you want to use in your request
var filterarray = [{
"name": "MaxPrice",
"value": "25",
"paramName": "Currency",
"paramValue": "USD"
},
{
"name": "FreeShippingOnly",
"value": "true",
"paramName": "",
"paramValue": ""
},
{
"name": "ListingType",
"value": ["AuctionWithBIN", "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() {
alert("buildURLArray working");
// 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 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];
}
}
}
}
} // End buildURLArray() function
// Execute the function to build the URL filter
buildURLArray(filterarray);
</script>
HTML:
<h1>eBay Search Results</h1>
<div id="results"></div>
有两个警报:
alert(url)
返回了所有内容,除了urlfilter
是undefined
alert("buildURLArray working");
没有出现
Eclipse 为第 document.body.appendChild(s);
行返回错误,提示它为空。
Uncaught TypeError: cannot call method 'appendChild' of null
大部分代码来自教程,我不知道为什么我没有得到相同的结果。
在URL构造代码中,由于解释器的顺序性质,没有定义urlFilter;分配没有被提升,即简单地说,你不能在现在使用它的未来价值。所以只需剪切从 var URL= ...(第一行)到 document.body.appendChild(s) 的代码;并将其粘贴到脚本的末尾(在 buildURLArray(filterarray) 之后)tag.Now urlFilter 将获得一些值,之后将用于 URL 构造。