简体   繁体   English

从Azure / oauth2 / token端点使用jQuery,Ajax或AngularJS获取access_token时出现跨域问题

[英]Cross-origin issue while getting access_token using jQuery, Ajax or AngularJS from Azure /oauth2/token endpoint

I am trying to generate access_token from Azure. 我正在尝试从Azure生成access_token

Error Message: 错误信息:

Failed to load https://login.microsoftonline.com/......../oauth2/token : No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载https://login.microsoftonline.com/......../oauth2/token :所请求的资源上没有'Access-Control-Allow-Origin'标头。 Origin ' http://localhost:61697 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:61697 '。

Code snippet: 程式码片段:

function SubmitForm() {
    var data = {
        'grant_type': "password",
        'client_id': "<client_id>",
        'username': "<username>@<tenant>.onmicrosoft.com",
        'password': "<password>",
        'resource': "<resource>",
        'redirect_uri': "http://localhost:55871/"
    }

    $.ajax({
        url: "https://login.microsoftonline.com/<tenant_id>/oauth2/token",
        type: "POST",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        crossDomain: true,
        beforeSend: setHeader,               
        success: function(d) {
            console.log(d);
        },
        error: function (a, b, c) {
            console.log(a, b, c);
        }
    });
}

Similarly I tried for jQuery and AngularJS as well but fall into the same error. 同样,我也尝试过使用jQuery和AngularJS,但遇到了相同的错误。

客户端问题

从服务器端收到响应

Can you help us in getting an access_token along with refresh_token from Azure using any client side scripting which is browser independant? 您可以帮助我们使用与浏览器无关的任何客户端脚本从Azure获取access_tokenrefresh_token吗?

Note: The same piece of code is only working with IE (not with Chrome, Edge, Safari etc.). 注意:同一段代码仅适用于IE(不适用于Chrome,Edge,Safari等)。

Refer to: No 'Access-Control-Allow-Origin' header with Microsoft Online Auth 请参阅: Microsoft Online Auth中没有“ Access-Control-Allow-Origin”标头

Perhaps you're getting the issue because you're accessing it from localhost. 可能是因为您正在从本地主机访问它而遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM