Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 使用Jquery获取web api的访问令牌_Ajax_Rest_Asp.net Web Api_Access Token - Fatal编程技术网

Ajax 使用Jquery获取web api的访问令牌

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>> {

我有一个基于令牌身份验证的WEB API。当我使用HTTP客户端在控制台应用程序中发出请求时,它会正确执行。要执行的代码是

   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.
});