javascript - Confusion on data returned from controller to Ajax -
i have ajax call in 1 of coffee scripts goes method of controller. ajax call looks this:
auto = -> $.ajax url : '<method_name>' type : 'post' data : <variable_name>: $('element').val() success: (data, status, response) -> alert('pass') error: -> alert('fail') my method in controller looks as:
def method ... ... <book> object can invalid object. respond_to |format| format.json { render json: {book: {title: title}} } end end now, 'book/title'(from controller) should available in coffee script through 'data' variable(in coffee script). right?
my question when go in 'error' block(for coffee scripts)? can see 'book' object null(in controller) still coming in 'success' block(in coffee script).. thought if json objects null, come 'error' block.
whenever ajax fails (for eg. if there's routing error or error in controller's method etc) comes error block. here method works fine, without error, no matter book null or not. can check in controller if book object null, return flag , check variable in ajax success. example:
def method ... ... <book> object can invalid object. if book.nil? format.json { render json: {is_data_null: true} } else respond_to |format| format.json { render json: {book: {title: title}} } end end end and in ajax success block, can check flag.
Comments
Post a Comment