首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从URL读取JSON数据(JavaScript)

从URL读取JSON数据(JavaScript)
EN

Stack Overflow用户
提问于 2018-08-16 05:09:34
回答 1查看 172关注 0票数 0

尝试2回答我的问题(请耐心等待--我是新来的)。

我正在尝试从URL读取数据

http://test.fhir.org/r3/Patient?family=smith&given=peggy&_format=json

并希望使用Javascript将其重新格式化为表格

(1)我读不懂,

(2)我无法将其格式化为json2table.com中的表格

有人能帮上忙吗?这是我正在尝试的代码。

代码语言:javascript
复制
    <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>Playing with JSON</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">

      <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

      <style type="text/css">
        .row{ margin-top: 20px}
      </style>

      <script type="text/javascript">
        $(window).load(function(){

        console.log('loading event handlers');
        var $response = document.getElementById("response");
        var $getReposBtn = document.getElementById("get-repos");
        $getReposBtn.onclick = function(){
            var xhr = new XMLHttpRequest();
            xhr.timeout = 2000;
            xhr.onreadystatechange = function(e){
                console.log(this);
                if (xhr.readyState === 4){
                    if (xhr.status === 200){
                        // debugger;
                        // console.log(xhr.response);
                        $response.innerHTML = xhr.response;
                    } else {
                        console.error("XHR didn't work: ", xhr.status);
                    }
                }
            }
            xhr.ontimeout = function (){
                console.error("request timedout: ", xhr);
            }
            xhr.open("get", "http://test.fhir.org/r3/Patient?family=smith&given=peggy&_format=json", /*async*/ true);
            xhr.send();
        }
        });
    </script>

    </head>
    <body>
      <div class="container">
          <div class="row">In Development</div>
        <div class="row">
            <button id="get-repos">JSON download trial!!</button>
        </div>
        <div class="row">
            <pre id="response"></pre>
        </div>
    </div>

      <script>
        if (window.parent && window.parent.parent){
          window.parent.parent.postMessage(["resultsFrame", {
            height: document.body.getBoundingClientRect().height,
            slug: "9f7sb409"
          }], "*")
        }
      </script>
    </body>
    </html>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51866478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档