【问题标题】:unable to get response from rest api using XMLHttpRequest无法使用 XMLHttpRequest 从 rest api 获得响应
【发布时间】:2018-06-09 03:07:20
【问题描述】:

我想使用 XMLHttpRequest 调用 get rest api,但出现错误 -

“未捕获的语法错误:JSON 输入意外结束”。

rest api json 响应数据

{
    "timestamp": "2018-06-08T16:52:50.509Z",
    "dataFrame": "AQAAKCoAAQgFKgABBg==",
    "fcnt": 825,
    "freq": 865572000,
    "port": 2,
    "rssi": -115,
    "snr": -16,
    "sf_used": 12,
    "id": 1528476770509,
    "dr_used": "SF12BW125",
    "decrypted": true
}

代码

 <script>
        function UserAction() {
    var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "url", true);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.setRequestHeader("Authorization", "Basic a2Vyb==");
        xhttp.send();
    var response = JSON.parse(xhttp.responseText);
}
    </script>

编辑:

代码

 <script>
     function userAction() {
        let xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                let response = JSON.parse(xhttp.responseText);
                var dataFrame = response.dataFrame;
                /** code that handles the response **/
            }
        };
        xhttp.open("GET", "url", true);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.setRequestHeader("Authorization", "Basic a2VybmVsc==");
        xhttp.send();
    </script>

HTML

 <button type="submit" onclick="userAction()">Search</button>
   <h1 id="data"></h1>

【问题讨论】:

标签: javascript


【解决方案1】:

您应该为您的请求收听XMLHTTPRequest.onreadystatechange 事件,以便仅在请求完成后解析结果:

functio userAction() {
  let xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      let response = JSON.parse(xhttp.responseText);
      document.getElementById("data").innerHTML = response.dataFrame;
    }
  };
  xhttp.open("GET", "filename", true);
  xhttp.setRequestHeader("Content-type", "application/json");
  xhttp.setRequestHeader("Authorization", "Basic a2Vyb==");
  xhttp.send();
}

【讨论】:

  • 出现错误 - 未捕获的 DOMException:无法在“XMLHttpRequest”上执行“setRequestHeader”:对象的状态必须打开。
  • 对,我只是把顺序弄乱了一点,正在编辑。请告诉我它现在是否有效:)
  • 不错,也请把问题标记为已回答:-)
  • hai @Luca 你能告诉我如何将我的dataFrame变量绑定到我的html H1标签吗?
【解决方案2】:

您需要将 xhttp.onreadystate 属性设置为一个回调函数来处理各种状态,然后在收到响应时进行处理。

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange

【讨论】:

    【解决方案3】:

    您应该在输出之前检查请求的状态。

    function UserAction() {
       var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "url", true);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.setRequestHeader("Authorization", "Basic a2Vyb==");
        
        xhttp.onload = function(){
            if(xhttp.status ==200){
                var response = JSON.parse(xhttp.responseText);
            }
        }
        
        xhttp.send();
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2014-06-06
      • 2020-08-25
      • 2022-11-23
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多