( function( $ ) {
function CleanQueryString( query )
{
return encodeURI( query );
};
function ConcatData( settings )
{
settings.concatUrl = settings.googleUrl;
settings.concatUrl = settings.concatUrl.replace( "{key}", settings.googleApiKey );
settings.concatUrl = settings.concatUrl.replace( "{country}", settings.country );
settings.concatUrl = settings.concatUrl.replace( "{query}", settings.cleanQuery );
};
$.fn.GoogleSearchResult = function( options ) {
var settings = $.extend( {
query: null,
googleApiKey: "myapikey",
googleUrl: "https://www.googleapis.com/shopping/search/v1/public/products?key={key}&country={country}&q={query}&alt=json",
concatUrl: "",
country: "UK",
cleanQuery: ""
}, options);
return this.each( function() {
if( settings.query )
{
var $t = $(this);
settings.cleanQuery = CleanQueryString( settings.query );
ConcatData( settings );
alert( settings.concatUrl ); // This alerts the correct url and I've checked that it returns json
$.getJSON( settings.concatUrl, function( data ) {
alert("hi"); // This never alerts
$t.html( data );
});
}
return false;
} );
};
} )( jQuery );
我无法让我的 $.getJSON 工作..任何想法为什么它可能不会返回任何东西:
https://developers.google.com/shopping-search/v1/getting_started
当我直接访问它时,我发送的 url 返回正确的数据。