如何从URL(JavaScript)读取JSON数据?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (774)

以下是我使用的代码:

    <!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>
提问于
用户回答回答于

可以这样试试:

<!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">
  <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>
</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>

  <script>
    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>
</body>

</html>

<!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 src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
  <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>
</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>

  <script>
    $(function () {
      console.log('loading event handlers');

      $("#get-repos").click(function onClick() {
        var url = "http://test.fhir.orgs/r3/Patient?family=smith&given=peggy&_format=json";

        $.getJSON(url, function onSuccess(response) {
          $("#response").text(JSON.stringify(response));
        });
      });
    });
  </script>
</body>

</html>

扫码关注云+社区

领取腾讯云代金券