【问题标题】:Load mysql query after pageload页面加载后加载mysql查询
【发布时间】:2018-03-23 03:40:57
【问题描述】:

我有一些非常大的查询,我只想在页面加载完成后运行它们。

这是我尝试过的。

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(window).load(function() {
            $("#query").load("sql-query.php");
        });
    </script>
</head>

<div id="query">Loading...</div>

sql-query.php

$data = DB::table('tapplicant')->limit(5000)->get();
var_dump($data);

这个想法是在页面加载后返回查询数据。

【问题讨论】:

  • @Amani ,我是 Javascript 和 Ajax 的新手,我该怎么做,或者您能否向我推荐一些文档,谢谢您的帮助
  • 你可以用jQuery.ready()代替.load()
  • 我重新构建了您的案例,但出现此错误:TypeError: a.indexOf is not a function 所以您的问题是 jquery 版本..您不能再使用 $(window).load(function() {}); 改用 $(window).on("load", function (e) {}) --> 请参阅:jquery.com/upgrade-guide/3.0/…

标签: javascript


【解决方案1】:

我建议在document.ready() 之后使用jQuery.get()

 $( document ).ready(function() {
     $.get( "sql-query.php", function( data ) {
         $( "#query" ).html( data );
     });
 });

【讨论】:

  • 谢谢,如果我调用 html 文件而不是 php 文件,这项工作就可以了
【解决方案2】:

版本不匹配"Uncaught TypeError: a.indexOf is not a function" error when opening new foundation project

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        /*
        $( window ).load(function() {
          $("#query").load("sql-query.php");
        });         
        */
        $(window).on('load', function(){ 
            $("#query").load("sql-query.php");
        });
    </script>
</head>

<div id="query">Loading...</div>

建议您使用文档准备功能(即$(function())而不是窗口加载document.ready vs window

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#query").load("sql-query.php");
        });
    </script>
</head>

<div id="query">Loading...</div>

【讨论】:

    【解决方案3】:

    这是由于在此 JQuery 版本中所做的一些更改..

    改变一下

    $(window).load(function() {});

    $(window).on("load", function (e) {}); 并保持其余部分不变。

    详情请咨询this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多