简体   繁体   English

在主体加载之前完成 ajax 调用

[英]Complete ajax call before body loads

I read in w3schools that there are several ways an external script can be executed:我在 w3schools 上读到有几种方法可以执行外部脚本:

  1. If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)如果存在异步:脚本与页面的其余部分异步执行(脚本将在页面继续解析时执行)
  2. If async is not present and defer is present: The script is executed when the page has finished parsing如果不存在 async 而存在 defer:当页面完成解析时执行脚本
  3. If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page.如果既不存在 async 也不存在 defer:在浏览器继续解析页面之前,将立即获取并执行脚本。

So, if i understood correctly, since i have the following script in the head section of my html file without using async or defer it should first be executed and then the browser will continue parsing.所以,如果我理解正确的话,因为我在我的 html 文件的头部部分有以下脚本而没有使用 async 或 defer 它应该首先被执行然后浏览器将继续解析。

<script type="text/javascript" src="js/display.js"></script>

display.js显示.js

$.ajax({
    url:"fetch_items.php"
});

fetch_items.php fetches some items from the database and puts them in an array $_SESSION("my_array") . fetch_items.php 从数据库中获取一些项目并将它们放入数组$_SESSION("my_array")中。

So inside the body section of the html folder i use the values that reside in $_SESSION("my_array") to show some content.因此,在 html 文件夹的正文部分中,我使用$_SESSION("my_array")中的值来显示一些内容。

<?php 
if(isset($_SESSION["my_array"]))
{
?>
 <!-- html code echoing values from session -->
<?php
}
?>

but $_SESSION["my_array"] isn't set yet so the content doesn't appear.$_SESSION["my_array"]尚未设置,因此内容不会出现。 (if i reload the page the content will appear correctly) (如果我重新加载页面,内容将正确显示)

Why is this happening?为什么会这样? How can i ensure that the script has completed it's execution before the body loads?我如何确保脚本在正文加载之前已完成执行? is the following refers only to scripts: If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page.以下仅指脚本:如果异步或延迟均不存在:在浏览器继续解析页面之前,将立即获取并执行脚本。

AJAX stands for "Asynchronous JavaScript and XML". AJAX代表“异步JavaScript和XML”。 So it makes asynchronous request from your browser by running your javascript code. 因此,它通过运行JavaScript代码从浏览器发出异步请求。 Your current code makes request from browser to server. 您当前的代码将请求从浏览器发送到服务器。 Then server saves your array to session by using your PHP code and not reloads the page. 然后,服务器使用您的PHP代码将数组保存到会话中,而不重新加载页面。 That is why your array not printing immediately. 这就是为什么阵列无法立即打印的原因。

Bad solution: Refreshing page on success method of your ajax request. 错误的解决方案:刷新有关ajax请求成功方法的页面。

Better solution: Don't keep your array in session if it is not really necessary. 更好的解决方案:如果没有必要,不要将阵列保持在会话状态。 Return your array in the method at "fetch_items.php". 在“ fetch_items.php”中的方法中返回数组。 Then print the array using success callback of the ajax request. 然后使用ajax请求的成功回调打印数组。

Here is an example for usage of success and print method: Ajax response inside a div 这是使用成功和打印方法的示例: div中的Ajax响应

For more information you can read jquery api documentation: http://api.jquery.com/jquery.ajax/ 有关更多信息,您可以阅读jquery api文档: http : //api.jquery.com/jquery.ajax/

尝试在ajax调用参数中放入async : false

This is because of asynchronous calls.这是因为异步调用。 I encountered similar issue and fixed it by adding async:false in AJAX .我遇到了类似的问题并通过在AJAX中添加async:false来修复它。 You may see the below code fragment I used.您可能会看到我使用的以下代码片段。

 $.ajax({
        type: "GET",
        url: "fetch_items.php",
        data: {},
        async : false, // make it false
        success: function (html) {
            alert (html);
        }, error: function (error) {
            alert (error);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM