【问题标题】:How to get JSON data from Restful API URL?如何从 Restful API URL 获取 JSON 数据?
【发布时间】:2017-08-01 15:53:53
【问题描述】:

我想获取 'http://localhost:8080/api/printer?exclude=temperature' [HTTP/1.1] 提供的 Json 数据。

这是要提供的Json数据。

   {
  "state": {
    "text": "Operational",
    "flags": {
      "operational": true,
      "paused": false,
      "printing": false,
      "sdReady": true,
      "error": false,
      "ready": true,
      "closedOrError": false
    }
  }
}

类型是'GET',Content-Type 是'application/json'

以下是我的 JSP 完整源代码。

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" language="javascript">
function print_stat(){
     jQuery.ajax({
      type: 'GET',
      async: false ,
      url: 'http://localhost:8080/api/printer?exclude=temperature&apikey=1234567...',
      contentType: 'application/json; charset=utf-8',
      success: function (data) {
        alert("TEST");
      },
      error : function(x, e) {
          alert('server error occoured');
          if(x.status==0){ alert('0 error'); 
          }else if(x.status==404){ alert('404 error'); 
          }else if(x.status==500){ alert('500 error'); 
          }else if(e=='parsererror'){ alert('Error.nParsing JSON Request failed.'); 
          }else if(e=='timeout'){ alert('Time out.'); 
          }else { alert(x.responseText); }
        }
    });
}
 </script>
 <body>
 <input type="button" onclick="print_stat()" value="test">
 </body>

我看不到“测试”弹出窗口。 什么都没有发生。

我应该如何获取数据?

请帮帮我。

【问题讨论】:

  • async: false ,
  • 所以开发者控制台没有报错?
  • 你是否包含了 jquery?听起来不像。
  • 您的代码适用于我自己的网址。你的网址中的那些点是什么?
  • 该按钮是否在导致页面重新加载的表单中?

标签: javascript jquery ajax rest jsp


【解决方案1】:

你可以试试这个,

语法:

$.ajax({
    beforeSend: function(xhrObj){
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Accept","application/json");
    },
    type: "POST",
    url: uri,       
    data: jsonStrJson,               
    dataType: "json",
    success: function(json){
       console.log(json);
    }
});

你的代码应该是这样的 100% 工作和测试的代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" language="javascript">
function print_stat(){
     jQuery.ajax({
      type: 'GET',
      async: false ,
      url: 'http://localhost:8080/api/printer?exclude=temperature&apikey=1234567...',
      dataType: 'json',
      success: function (data) {
        alert("TEST");
        console.log(data);
      },
      error : function(x, e) {
          alert('server error occoured');
          if(x.status==0){ alert('0 error'); 
          }else if(x.status==404){ alert('404 error'); 
          }else if(x.status==500){ alert('500 error'); 
          }else if(e=='parsererror'){ alert('Error.nParsing JSON Request failed.'); 
          }else if(e=='timeout'){ alert('Time out.'); 
          }else { alert(x.responseText); }
        }
    });
}
 </script>
 <body>
 <input type="button" onclick="print_stat()" value="test">
 </body>
</body>
</html>

【讨论】:

  • 在 GET 上设置 contentType 标头毫无意义...没有请求内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 2018-08-24
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
相关资源
最近更新 更多