使用jquery ajax调用

时间:2018-11-04 09:18:39

标签: jquery ajax asp.net-mvc oauth-2.0 asp.net-web-api2

Error received while generating token

我在解决方案中使用了MVC Web和Web API项目。 我已经在Web API项目中实现了OAuth承载令牌生成,并且还启用了CORS。我已经尝试过从邮递员那里产生承载令牌并可以正常工作。我也尝试从Web项目中调用控制器的动作,并且它可以工作。但是,当我尝试使用jquery ajax从Web项目生成令牌时,出现了CORS错误。

下面是jquery ajax调用

var body = {             'grant_type':'密码',             '用户名':'xyz@domain.com',             'password':'password'         };

    $.ajax({
        url: 'http://localhost:54861/token',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        data: body,
        crossDomain: true,
        success: function (result) {
            console.log(result);
        },
        error: function (result) {
            console.log(result);
        },
    });

我在Web api项目OAuthAppProvider => GrantResourceOwnerCredentials方法中得到调试器,但没有得到返回结果。

如果我在同一个Web API项目的HTML页面中使用上述jquery ajax调用,那么它将起作用。 但是我想从Web项目中生成令牌。

0 个答案:

没有答案