【问题标题】:How to acsess another page by JS [closed]如何通过JS访问另一个页面[关闭]
【发布时间】:2020-04-14 13:13:39
【问题描述】:

我只想复制shizyab.ir/rand.php 中的所有texes 并通过javascript 将其粘贴到我的html 文件中。我该怎么办?

【问题讨论】:

    标签: javascript html dom copy copy-paste


    【解决方案1】:

    这是一个不需要jquery的版本:

    function insertPage() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            var element = document.createElement("div");
            element.innerHTML = this.responseText;
            document.body.appendChild(element);
        };
        xhttp.open("GET", "https://www.shizyab.ir/rand.php", true);
        xhttp.send();
    }
    

    并运行此代码:insertPage();

    【讨论】:

      【解决方案2】:

      您可以通过在window.onload 上的脚本中使用带有 jquery 的 ajax 调用来做到这一点: 假设你的 php 脚本返回一个 json 对象。

      function getData(username, callback) {
          var url = 'shizyab.ir/rand.php';
          $.ajax({
              url:(url),
              type: 'GET',
              dataType: 'json',
              success: (data) => {
                 callback(data);
              }
          });
      }
      

      然后在callback函数中处理你的数据显示。

      【讨论】:

      • 这使用了 jquery。纯原生解决方案会更好
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2019-03-14
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多