繁体   English   中英

Ajax 调用以使用 oath2 检索身份验证代码

[英]Ajax call to retrieve auth code using oath2

我正在使用一些 API 点设置来获取数据。 我必须点击的第一个端点返回一个身份验证代码,该代码将传递到调用 header 的 rest 中。

我似乎在使用 ajax 打第一个电话时遇到问题,它与ajax配合postman 我不断收到错误消息,但没有消息。

我是否正确设置? 它正在使用 oauth2,我对此没有任何经验。

一个 jsfiddle 示例https://jsfiddle.net/7f5dgn08/

var url = 'http://webaddress';

 $.ajax({
    type: "oauth2",
    url: url,
    cache: false,
    async: true,
    crossDomain: true,
    headers: {
        username: "username",
        password: "pass"
    },
    dataType: "json",
     success: function (data) {

        localStorage.setItem('aCode', JSON.stringify(data))
    },
     error: function (jqXHR, exception) {
        console.log(jqXHR.responseJSON);
    }
 });

如果它在 postman 中有效,但在浏览器中无效,则可能是发生了 CORS 错误。 如果调用的Request MethodPOSTOPTION ,请查看浏览器的开发工具。

如果动词是OPTION ,您可能会收到没有响应正文的错误,但您还会在控制台中获得与 CORS 问题相关的日志。

If the error is CORS related you have to add CORS policies to your endpoint, and in the response header a Access-Control-Allow-Origin header that has as value the address where your frontend is located.

暂无
暂无

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

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