简体   繁体   English

jQuery AJAX请求不适用于页面加载,但是可以通过调试控制台运行

[英]jQuery AJAX Request Doesn't Work on Page Load, However, It Does Work from Debug Console

Turning off asynchronous requests in jQuery fixed the issue. 在jQuery中关闭异步请求解决了该问题。

I have the following Javascript & AJAX request (using jQuery) in my page: 我的页面中有以下Javascript和AJAX请求(使用jQuery):

    "use strict";

    var hsArea, counter, hotspots, count;
    counter = 4;
    count = 0;
    hotspots = {};

    function fetchHotspotList() {
        $.getJSON ('/alpha/engine/hotspots/gethotspot.php', {'type' : 'list'}, function(json) {
            hotspots = json;
        });
    }

    function displayHotspot(type, id, number) {
        $.ajax({
            url: '/alpha/engine/hotspots/gethotspot.php',
            dataType: 'json',
            data: {'type' : type, 'id' : id},
            success: function(json) {
                console.log(json);
                var hotspot, extract;
                    extract = json.content;
                    extract = extract.replace(/<(?:.|\n)*?>/gm, '');
                    extract = extract.substring(0, 97);
                    extract = extract + "...";
                    json.content = extract;

                    hotspot = document.createElement("div");
                    hsArea.append(hotspot);
                    hotspot.setAttribute('class','hotspot');
                    hotspot.setAttribute('id','hotspot' + number);

                    $(hotspot).css('position', 'absolute');
                    $(hotspot).css('top', number * 100 + 100);
                    $(hotspot).css('left', number * 100 + 110);

                    hotspot.innerHTML = "<h1>"+ json.title + "</h1><p>" + json.content + "</p>";
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus, errorThrown);
            }
        });
        }

        function listHotspots() {
            for(count = 0; count < counter; count++) {
                (function(count) {
                    displayHotspot('scribble',hotspots[count], count);
                    count = count + 1;  
                })(count);
            }
        }

        function loadHotspots() {
            fetchHotspotList();
            listHotspots();
        }

    $(document).ready(function() {
        hsArea = $("#hotspotArea");
        fetchHotspotList();
        listHotspots();
    });

(Sorry the formatting is a bit off!) - Now, the $(document).ready() function assigns the hsArea variable as it should, however, a combination of fetchHotspotList() and listHotspots() returns: (抱歉,格式设置有点过关!)-现在,$(document).ready()函数将hsArea变量分配为应有的值,但是fetchHotspotList()和listHotspots()的组合返回:

Uncaught TypeError: Cannot call method 'replace' of null

However, if in the Google Chrome Javascript console, I run: 但是,如果在Google Chrome Javascript控制台中运行:

loadHotspots();

it fetches the data from the AJAX request and displays it properly on the page. 它从AJAX请求中获取数据,并在页面上正确显示。 At first I thought the problem was that I Wasn't using the $(document).ready() handler, but adding it hasn't fixed it. 起初我以为问题是我没有使用$(document).ready()处理程序,但是添加它并没有解决它。 Neither has using an onload handler inside of the body tag. 两者都没有在body标签内部使用onload处理程序。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Regards, Ben. 问候,本。

It's probably due to the fact that your listHotSpots function is called before fetchHotSpots returns (since it's an async call). 可能是由于在fetchHotSpots返回之前调用了listHotSpots函数(因为它是一个异步调用)。

You're better off chaining the execution of listHotSpots to the completion of fetchHotSpots , like so: 最好将listHotSpots的执行与listHotSpots的完成联系fetchHotSpots ,如下所示:

function fetchHotspotList() {
    $.getJSON ('/alpha/engine/hotspots/gethotspot.php', {'type' : 'list'}, function(json) {
        hotspots = json;
        listHotSpots();
    });
}

You may be better off modifying listHotSpots to take the json data returned from your AJAX call. 您最好修改listHotSpots以获取从AJAX调用返回的json数据。 Hope this helps! 希望这可以帮助!

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

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