Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.ajax json return on error

Tags:

jquery

ajax

$.ajax({
    type: 'POST',
    url: api_url+'client/'+client.id+'.json', 
    data: {
        _method: 'delete',
        id: client.id
    },
    success: function(data) {
        $('#delete-client').html('Success');
    },
    error: function(data) {
        $('#delete-client').css('color', 'red');
        $('#delete-client').html('Error');
    }
});

On the error: function the jquery would recieve this json object with a 500 header status

{"errors":{"code":777,"message":"Method requested does not yet exist","data":[]}}

however if I use data.errors.message it doesnt show the error there. It shows a huge object with different events in chromes developer box when I console.log the return object its using

FIXED

var error = jQuery.parseJSON(jqXHR.responseText);
$('#delete-client').html(error.errors.message);
like image 476
azz0r Avatar asked Oct 22 '11 21:10

azz0r


People also ask

How can we handle exception handling in Ajax?

Example: We are going to see how to use AJAX fail() methods to handle the error in the HTTP requests. The fail() callback takes 3 parameters where the first parameter is a JSON error object, the second parameter is given a reason in text format and the last parameter is for the error thrown by the HTTP request.

What is success function Ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

How can make AJAX call in jQuery?

The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Parameters: The list of possible values are given below: type: It is used to specify the type of request. url: It is used to specify the URL to send the request to.


1 Answers

add : dataType:"json"...............

like image 181
Royi Namir Avatar answered Oct 04 '22 15:10

Royi Namir