getJSON和Bearer令牌

问题描述 投票:0回答:1

我有一个工作的getJSON脚本来输出JSON字符串。但是使用Bearer Token api它可以工作。

在我的Http Header中已包含..授权:Bearer Token

有没有人有我的想法?

    <div id = "stage" style = "background-color:#cc0;">
         STAGE
      </div>

      <input type = "button" id = "driver" value = "Load Data" />

<script>
         $(document).ready(function() {

            $("#driver").click(function(event){
               $.getJSON('https://www.myapiwithbtoken.com/result.json', function(jd) {
                  $('#stage').html('<p> Name: ' + jd.name + '</p>');
                  $('#stage').append('<p>Age : ' + jd.age+ '</p>');
                  $('#stage').append('<p> M: ' + jd.m+ '</p>');
               });
            });

         });
      </script>
jquery json getjson bearer-token
1个回答
0
投票

在发出请求时,您将要使用其他方法将承载令牌附加到标头。使用您提供的代码,它似乎不会这样做。查看$ .ajax方法,下面是一个发出GET请求的示例。

$.ajax({
    type: "GET", //GET, POST, PUT
    url: '/authenticatedService'  //the url to call
    contentType: contentType,           
    beforeSend: function (xhr) {   //Set token here
        xhr.setRequestHeader("Authorization", 'Bearer '+ token);
    }
}).done(function (response) {
    //Response ok. process reuslt
}).fail(function (err)  {
    //Error during request
});
© www.soinside.com 2019 - 2024. All rights reserved.