views:

52

answers:

4

In "get_data" action ,there are some code like this:

def get_data
 if params[:p]=='1'
   raise "error 1"
 elsif params[:p]=='2'
   raise 'error 2'
 else
  return data
 end
end

in view:

<script>
 $.getJSON('/controller/get_data',function(){...})
</script>

so,when some error has raised,how can i get it! Tks!

A: 

you are looking for the .ajaxError() function

jAndy
+1  A: 

http://api.jquery.com/ajaxError/

Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.

abrereton
+3  A: 

One thing to remember is.

getJson is just a thin wrapper for:

getJSON: function( url, data, callback ) {
    return jQuery.get(url, data, callback, "json"); 
},

Which in turn is a thin wrapper for get:

 get: function( url, data, callback, type ) {
    // shift arguments if data argument was omited
    if ( jQuery.isFunction( data ) ) {
        type = type || callback;
        callback = data;
        data = null;
    }

    return jQuery.ajax({
        type: "GET",
        url: url,
        data: data,
        success: callback,
        dataType: type
    });
},

Which simply boils down to a call to $.ajax, which has a simple failure and success handler.

So you could simply do:

$.ajax({
    type: "GET",
    url: url,
    data: data,
    success: callback,
    dataType: "json", 
    error: failureCallback
});
Sam Saffron
Thank you for your detail instructions,and i use $.ajax method
psjscs
A: 

Thanks all,i've solved the problem !~

psjscs