【发布时间】:2021-11-07 12:16:39
【问题描述】:
我可以像这样从我的 Openiddict 服务器获取令牌:
var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:7000/connect/token");
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "password",
["username"] = email,
["password"] = password
});
var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
var tempt1 = await response.Content.ReadAsStringAsync();
但是当我通过 jQuery 和邮递员调用时,我得到了错误:
var body = {
grant_type: 'password',
username: email,
password: password
};
$.ajax({
url: 'https://localhost:7000/connect/token',
crossDomain: true,
type: 'POST',
dataType: 'json',
contentType: 'application/json; 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");
},
});
我做错了什么。请告诉我。谢谢
【问题讨论】:
-
您遇到的错误是什么?开发工具控制台中有什么吗?
-
@RaviKumarGupta 错误请求 400
-
这意味着发送的数据不是预期的。我建议比较一下。尝试您的工作解决方案并在浏览器中查看传出数据并将其与 JQuery 解决方案进行比较。
-
我使用 c# 调用 API 获取令牌,它的工作,但我不知道我做错了什么 JQuery。
标签: jquery ajax openiddict