Ajax 使用Jquery获取web api的访问令牌
我有一个基于令牌身份验证的WEB API。当我使用HTTP客户端在控制台应用程序中发出请求时,它会正确执行。要执行的代码是Ajax 使用Jquery获取web api的访问令牌,ajax,rest,asp.net-web-api,access-token,Ajax,Rest,Asp.net Web Api,Access Token,我有一个基于令牌身份验证的WEB API。当我使用HTTP客户端在控制台应用程序中发出请求时,它会正确执行。要执行的代码是 var request = new HttpRequestMessage(HttpMethod.Post, apiBaseUrl + "token"); var keyValues = new List<KeyValuePair<string, string>> {
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;
我认为您需要将ajax类型更改为post和content类型, contentType:'应用程序/x-www-form-urlencoded;字符集=UTF-8', 有关更多详细信息,请参阅下一期, 谢谢,
Nagaraj M.它不是在POST类型中默认设置的吗?
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.
});