I am having a WEB API with Token based Authentication. When i make a request in console application using HTTP client its properly executed. The code to execute is
var request = new HttpRequestMessage(HttpMethod.Post, apiBaseUrl + "token");
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret)
};
request.Content = new FormUrlEncodedContent(keyValues);
HttpClient httpClient = new HttpClient();
var response = httpClient.SendAsync(request).Result;
var result = response.Content.ReadAsStringAsync().Result;
I want to make the same request using Ajax. But it shows bad request.
window.jQuery.ajax({
"url": "http://localhost:63297/token",
"type": "GET",
"dataType": "json",
"timeout": 10000,
"data": {
"client_id": "",
"client_secret": "",
"grant_type": "client_credentials"
}
}).done(function(data, textStatus, jqxhr) {
//Write code to be executed when the request SUCCEEDS.
}).fail(function(jqxhr, textStatus, errorThrown) {
//Write code to be executed when the request FAILS.
});
I think you need to change the ajax type to post and content type, contentType: 'application/x-www-form-urlencoded; charset=UTF-8', Also refer the below issue for further details, Request Token with JQuery from Web API
Thanks, Nagaraj M.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With