Just finishing the application, and I need to implement one more thing on the json side.
I have a jquery autocomplete that uses a web service that I wrote that returns json.
I need to change this bit a bit so that if the first request with parameters returns null, it will try again with the default URL without parameters, which performs the main search.
Just to be sure that I'm not missing any tricks, I said I would ask and see if any jquery guru has an elegant way to achieve this.
var cache = {},lastXhr; var web_service_url_default = "http://json_sample.php"; var web_service_url_specific = "http://json_sample.php/?param1=hello¶m2=world"; var autocomp_opt = { minLength: 1, source: function( request, response ) { var term = request.term; if ( term in cache ) { response( cache[ term ] ); return; } lastXhr = $.getJSON( web_service_url_specific, request, function( data, status, xhr ) { cache[ term ] = data; if ( xhr === lastXhr ) { response( data ); } }); } };
This is a parameter variable for autocomplete, which is entered automatically for complete completion, and which works fine.
$('.some_class').autocomplete(autocomp_opt);
I need to change it so that if the first request returns empty, it starts the default request with no parameters.
Help is welcomed, as always.
Updated with a working example.
After receiving this, see the following code if it helps anyone. It may not be the most elegant, but it still works.
Please note that in this example and during testing, caching seemed inactive and made the request immediately dismissed, so I deleted it. Now it works 100%.
var autocomp_opt = { minLength: 1, source: function( request, response ) { var term = request.term; lastXhr = $.getJSON( web_service_url_specific, request, function( data, status, xhr ) {