Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax() log the HTTP request

I have a function that sends an HTTP POST request and i want to log it for debugging purposes. Here is the function:

function serverRequest(URL, DATA, callback) {
    $.ajax({
        url: URL,
        type: "POST",
        dataType: "text",
        contentType: "text/xml",
        processData: false,
        data: DATA,
        success: function (response) {
            console.log(response);
            callback(response);
        },
        error: function (response) {
            console.log(response);
            callback(null);
        }
    });
}

How can i log the whole HTTP POST request (HTTP Header + data), as soon as it is send?

Thanks.

like image 689
Christos Baziotis Avatar asked Apr 02 '14 13:04

Christos Baziotis


People also ask

Are AJAX requests HTTP requests?

Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method.

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Can I console log in AJAX?

Using developer tool you can check for request and response of the ajax. You can also see console. log(response) where success function is written in ajax function.

Can AJAX be used with jQuery?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!


1 Answers

Look for the tab "Network" (not the Console tab) on your Developer Tools (Ctrl+Shift+J) if you are using Chorme, or anythig similar if you are using another browser.

Even after that, if you want to log the XHtmlRequest, you can always do (if your browser supports console.log):

var xhr = $.ajax(...);
console.log(xhr);

Hope I've helped.

like image 89
Walter Macambira Avatar answered Oct 02 '22 00:10

Walter Macambira