1
votes

I am trying to get access_token utilizing jQuery. Problem is, that I cannot get that token (server is running on localhost). Server works fine (I tried that with postman), but I cannot get it with jQuery. Browser writes after clicking on the button.

The resource from “http://localhost:8080/oauth/token?callback=jQuery34105901959820360243_1562175129954&grant_type=password&client_id=my-client&client_secret=my-secret&username=test%40seznam.cz&password=Peter&_=1562175129955” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).

jQuery function to get access_token

function authenticateUser(email, password) {
    var body = {
        grant_type: 'password',
        client_id: 'my-client',
        client_secret: 'my-secret',
        username: "[email protected]",
        password: "Peter"
    };


    $.ajax({
        url: 'http://localhost:8080/oauth/token',
        crossDomain: true,
        type: 'POST',
        dataType: 'jsonp',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        header: {"Access-Control-Allow-Origin": "*"},
        data: body,
        complete: function(result) {
            alert(result);
        },

        success: function(result) {
            alert(result + " OK!");
        },

        error: function(result) {
            alert(result + " CHYBA");
        },
    });
    return true;
}
1

1 Answers

0
votes

If the javascript file is served by the same server that gives out the tokens then there's no need to use the full url in your jquery ajax code (and there's no need to indicate crossDomain=true). It also seems that your server is expecting json content type instead of url-encoded

use

        url: '/oauth/token',
        crossDomain: false,
...
        contentType: 'application/json; charset=UTF-8',

To make the request


Edit

Trye this way:

   $.post("/oauth/token",body,
  function(data, status) {
    alert("Data: " + data + "\nStatus: " + status);
  });

Hope this helps