Laravel ajax setup for jQuery and Angular

Request::ajax() = True when:

headers:{
    "X-Requested-With" : "XMLHttpRequest"
},

Request::wantsJson() = True when:

Note: laravel looks for “json” in the first accept parameter.

headers:{
    "Accept" : "application/json"
}

Both are true if

headers:{
    "X-Requested-With" : "XMLHttpRequest",
    "Accept" : "application/json"
}

Global jQuery setup

This will add the proper headers to all jQuery$.ajax $.get $.post calls in your app.

 

$.ajaxSetup({
    headers: {
        "X-Requested-With" : "XMLHttpRequest",
        accept: "application/json"
    }
});

The headers can be overridden and appended in the indivdual calls.

Global Angular setup

This will add the proper headers to all Angular http calls in your app.

app.config(['$httpProvider', function($httpProvider){

    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

    $httpProvider.defaults.headers.common["accept"] = 'application/json';

}]);

 

Published by

Brian Gaeddert

A hard working quick learner who is an ambitious and self-motivated web developer with a strong technical and design background who possesses self-discipline and the ability to work with the minimum of supervision. A great team player who thrives on creating innovative solutions in a multidisciplinary team, is detail-oriented and has the ability to produce detailed technical specifications from client requirements. A quick learner who can absorb new ideas and communicate quickly and effectively. A high awareness of industry issues and trends, particularly in regard to emerging technologies, website usability/accessibility, mobile solutions, and data integration. Native English speaker with professional proficiency in Spanish.

Leave a Reply

Your email address will not be published. Required fields are marked *