【问题标题】:Get token from Openiddict by jQuery?通过 jQuery 从 Openiddict 获取令牌?
【发布时间】: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


【解决方案1】:

您的内容类型错误,您没有发送 JSON,因此请移除 contentType: 'application/json; charset=UTF-8',
此外,Access-Control 标头从服务器发送到客户端,而不是相反。

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 2018-02-11
    • 2016-11-28
    • 1970-01-01
    • 2016-04-20
    • 2016-02-08
    • 2017-06-12
    • 2021-05-04
    • 2015-03-05
    相关资源
    最近更新 更多