I am working on a simple eBay jQuery plugin. It succeeds in making the call. It returns a result. However, I get “Uncaught ReferenceError: _cb_findItemsByKeywords is not defined” when it tries to find my callback function.
(function( $ ){
$.fn.ebay_jQuery = function( options ) {
var settings = $.extend({
rootURL : "http://svcs.ebay.com/services/search/FindingService/v1",
operationName : "findItemsByKeywords",
serviceVersion : "1.0.0",
securityAppName : "myApp",
globalID : "EBAY-US",
responseFormat : "JSON",
callback : "_cb_findItemsByKeywords",
keywords : "religious%20book",
entriesPerPage : "20"
}, options);
var html = [];
// Parse the response and build an HTML table to display search results
var _cb_findItemsByKeywords = function(root) {
var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];
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('<div class="box item-box" style="position: absolute; opacity: 1; top: 0px; left: 240px; ">'+
'<div class="item">'+
'<div>'+
'<div class="goods-pic">'+
'<a href="' + viewitem + '" target="_blank">' +
'<img src="' + pic + '" border="0">'+
'</a>'+
'</div>'+
'<p class="item-title" style="margin-top: 10px;">'+
'<a href="' + viewitem + '" target="_blank">' + title + '</a>'+
'</p>'+
'<p class="item-price-fee clearfix"><span class="sale-price"> $38.5</span> / <span class="blue">0 bids</span></p>'+
'<p class="clearfix"><span class="blue pull-left" style="color: #538dc2;">0 mins left</span></p>'+
'<p><span class="blue">Buy it now ($52.5)</span></p>'+
'<p><span class="pull-right">Portland, TN</span></p>'+
'</div>'+
'</div>'+
'</div>');
}
}
}
var constructRequest = function(){
// Construct the request
url = settings.rootURL;
url += "?OPERATION-NAME="+settings.operationName;
url += "&SERVICE-VERSION="+settings.serviceVersion;
url += "&SECURITY-APPNAME="+settings.securityAppName;
url += "&GLOBAL-ID="+settings.globalID;
url += "&RESPONSE-DATA-FORMAT="+settings.responseFormat;
url += "&callback=_cb_findItemsByKeywords";
url += "&REST-PAYLOAD";
url += "&keywords="+settings.keywords;
url += "&paginationInput.entriesPerPage="+settings.entriesPerPage;
//url += urlfilter;
return url;
}
var submitRequest = function(){
var url = constructRequest();
// Submit the request
s=document.createElement('script'); // create script element
s.src= url;
document.body.appendChild(s);
}
// End _cb_findItemsByKeywords() function
return this.each(function() {
submitRequest();
$(this).append(html);
});
};
})( jQuery );
<html>
<head>
<title>eBay Search Results</title>
<script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/js/ebay_jQuery.js"></script>
<script>
$(document).ready(function(){
$('#results').ebay_jQuery();
});
</script>
</head>
<body>
<div id="main-content">
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="item-container clearfix" id="item-container" style="position: relative; height: 1204px; ">
<div id="results"></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
3
Answers
Your callback function isn’t defined on the global scope, therefore it cannot be reached by the calling script.
You will need to give each callback method a unique name and add it to the window.
Then pass that unique name to the ebay api as the desired callback fn for that script.
You can wrap the ebay call in a jquery ajax call, the response is automatically linked to the method _cb_findItemsByKeywords with the root variable filled.
There is an example page:
The answer provided alejo is actually working, however I would revise it a bit.
jQuery ajax call supports the dataType: ‘jsonp’, which adds ‘callback=?’ to the url by default, and allows you to define the callback function as a success attribute of the ajax call. You can also use the data attribute to pass the query params