You are currently viewing Process Ajax Request and Response Data in JavaScript/jQuery

Process Ajax Request and Response Data in JavaScript/jQuery


Ajax which stands Asynchronous JavaScript and XML is a method to send asynchronous request to the server for some operations. In JavaScript/jQuery , we can do ajax request through $.ajax function.
If the request in successful, we can do additional operation depending on it through success callback method. And if the request is failed, error callback method can be used.
Success callback and Error callback of a ajax request method depends on response status code.

Most common response status code for success request is 200. While error response status code includes 401, 401, 403, 404, 500 and so on.

Example of JavaScript/jQuery for handling Ajax request and response:

$.ajax({
    url: '/ajaxreq/',
    type: 'POST',
    data: {
        msg: CSRF_TOKEN,
    },
    dataType: 'JSON',
    success: function (data) 

        alert('Success');
},
    error: function (data) {

        alert('Error')
    }
});
JavaScript

Leave a Reply