Get event error in Jquery autocomplete remote datasource

error-handling, jquery, jquery-ui, jquery-ui-autocomplete

Solution

You could re-structure your widget to use a function as the `source` parameter, then make the AJAX request yourself and do whatever you'd like upon an error:

$(idObjeto).autocomplete({
    source: function (request, response) {
        $.ajax({
            url: url,
            dataType: "json",
            data: request,
            success: function (data) {
                response(data);
            },
            error: function () {
                response([]); // send no results to the widget.
                alert("an error occurred!");
            }
        });
    },
    minLength: 3,
    select:function(data,ui){
        $(formatIdJQuery(idObjValueReceptor)).val(ui.item.id);
    }
}).data( "autocomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>" + item.label + " - <strong>" + item.id + "</strong></a>" )
        .appendTo( ul );
    };
};

Problem

I am using Jquery autocomple with remote datasource, but, sometimes when try do search in my server a error is returned , because I'm using a rest webservice like datasource. I'd like to know what returned status code by my webservice and print a error message Example: ``` $(idObjeto).autocomplete({ source:url, minLength: 3, select:function(data,ui){ $(formatIdJQuery(idObjValueReceptor)).val(ui.item.id); } }).data( "autocomplete" )._renderItem = function( ul, item ) { return $( "<li></li>" ) .data( "item.autocomplete", item ) .append( "<a>" + item.label + " - <strong>" + item.id + "</strong></a>" ) .appendTo( ul ); }; } ``` Suppose what my webservice returned status code 404, I'd like to get this status code and call alert window, for example. That's all folks!

Original source