JWT token with jQuery Ajax

Lee picture Lee · Dec 1, 2015 · Viewed 9.4k times · Source

I have an API being driven with Laravel, Dingo and JWT Tokens. Testing the API call with PAW works perfectly. Running the API calls with jQuery without middleware JWT Tokens disable works fine. But as soon as I try running an Ajax request with JWT Tokens Im getting a 401.

Am I missing a trick with the Ajax Request. Can you see a problem with this code?

$.ajax({
    url: "http://api.domain.app/products",
    dataType : 'jsonp',
    type: 'GET',
    beforeSend : function(xhr) {
        xhr.setRequestHeader("Accept", "application/json");
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.setRequestHeader("Authorization", "Bearer XXXX");
    },
    error : function() {
        // error handler
    },
    success: function(data) {
        console.log(data);
        return data;
    }
});

I am having to use jsonp due to Cross Domain. But again this is working fine turing off the JWT middleware.

Hope you can advise..

Answer

Lee picture Lee · Dec 2, 2015

I removed the API from the subdomain and its working fine. It must have something to do with jsonp and JWT Tokens.