Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery getJSON&;不记名代币_Jquery_Json_Getjson_Bearer Token - Fatal编程技术网

Jquery getJSON&;不记名代币

Jquery getJSON&;不记名代币,jquery,json,getjson,bearer-token,Jquery,Json,Getjson,Bearer Token,我有一个工作的getJSON脚本来输出JSON字符串。 但如果使用承载令牌api,则无法正常工作 在我的Http头中包含了allready。。 授权:无记名Mytoken 有人对我有什么想法吗 <div id = "stage" style = "background-color:#cc0;"> STAGE </div> <input type = "button" id = "driver" value = "L

我有一个工作的getJSON脚本来输出JSON字符串。 但如果使用承载令牌api,则无法正常工作

在我的Http头中包含了allready。。 授权:无记名Mytoken

有人对我有什么想法吗

    <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>

阶段
$(文档).ready(函数(){
$(“#驱动程序”)。单击(函数(事件){
$.getJSON('https://www.myapiwithbtoken.com/result.json,函数(jd){
$('#stage').html('Name:'+jd.Name+'

'); $(“#阶段”)。追加(“Age:”+jd.Age+”

); $(“#stage”).append(“M:”+jd.M+”

”); }); }); });
当您发出请求时,您需要使用另一种方法将承载令牌附加到您的头上。使用您提供的代码,它似乎不会这样做。看看$.ajax方法,下面是一个发出GET请求的示例

您可以看到,您得到了一个“done”和“fail”回调方法来处理

$.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. Work with the data returned
         $('#stage').html('<p> Name: ' + response.name + '</p>');
         $('#stage').append('<p>Age : ' + response.age+ '</p>');
         $('#stage').append('<p> M: ' + response.m+ '</p>');

}).fail(function (err)  {
    //Handle errors here
         $('#stage').append('<p>error with fetching results</p>');
});
$.ajax({
键入:“GET”、//GET、POST、PUT
url:“/authenticatedService”//要调用的url
contentType:contentType,
beforeSend:function(xhr){//在此处设置令牌
xhr.setRequestHeader(“授权”,“承载人”+令牌);
}
}).完成(功能(响应){
//响应正常。处理返回的数据
$('#stage').html('Name:'+response.Name+'

'); $(“#stage”).append(“Age:”+response.Age+”

); $(“#stage”).append(“M:”+response.M+”

”); }).失败(功能(错误){ //在这里处理错误 $(“#stage”).append(“获取结果出错”

”); });
当您发出请求时,您需要使用另一种方法将承载令牌附加到您的头上。使用您提供的代码,它似乎不会这样做。看看$.ajax方法,下面是一个发出GET请求的示例

您可以看到,您得到了一个“done”和“fail”回调方法来处理

$.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. Work with the data returned
         $('#stage').html('<p> Name: ' + response.name + '</p>');
         $('#stage').append('<p>Age : ' + response.age+ '</p>');
         $('#stage').append('<p> M: ' + response.m+ '</p>');

}).fail(function (err)  {
    //Handle errors here
         $('#stage').append('<p>error with fetching results</p>');
});
$.ajax({
键入:“GET”、//GET、POST、PUT
url:“/authenticatedService”//要调用的url
contentType:contentType,
beforeSend:function(xhr){//在此处设置令牌
xhr.setRequestHeader(“授权”,“承载人”+令牌);
}
}).完成(功能(响应){
//响应正常。处理返回的数据
$('#stage').html('Name:'+response.Name+'

'); $(“#stage”).append(“Age:”+response.Age+”

); $(“#stage”).append(“M:”+response.M+”

”); }).失败(功能(错误){ //在这里处理错误 $(“#stage”).append(“获取结果出错”

”); });
这很有用,我可以看到控制台日志,其中记录了检索到的数据。但在此之后如何将数据追加到表中?当然,我将重新编辑此答案以说明如何执行此操作。这很有用,并且我可以看到检索数据的控制台日志。但在此之后如何在表中追加数据?当然,我将重新编辑此答案以说明如何执行此操作。