Thanks Prateek !
I have paste the Code blow for Access Token when any user hit my web-api.
This code not return any access token. I used all the Credentials in Postman tool , its return the token. Please check once .
function loadValidation() {
var key;
var token_ // variable will store the token
var clientID = "xyz"; // app clientID
var clientSecret = "abcd"; // app clientSecret
var url = "xyzabc.azurewebsites.net/.../token"; // Your application token endpoint
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Access-Control-Allow-Headers", "*");
request.setRequestHeader('Access-Control-Allow-Credentials', true);
request.setRequestHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
request.send("grant_type=client_credentials&client_id=" + clientID + "&" + "client_secret=" + clientSecret + "&audience=serviceapi"); // specify the credentials to receive the token on request
request.onreadystatechange = function () {
if (request.readyState == request.DONE) {
var response = request.responseText;
var obj = JSON.parse(response);
key = obj.access_token; //store the value of the accesstoken
token_ = key; // store token in your global variable "token_" or you could simply return the value of the access token from the function
}
}
}