doucha4054 2012-09-29 10:35
浏览 74
已采纳

jQuery AJAX请求在页面加载时不起作用,但是,它在调试控制台上有效

Turning off asynchronous requests in jQuery fixed the issue.

I have the following Javascript & AJAX request (using jQuery) in my page:

    "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(/<(?:.|
)*?>/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:

Uncaught TypeError: Cannot call method 'replace' of null

However, if in the Google Chrome Javascript console, I run:

loadHotspots();

it fetches the data from the AJAX request and displays it properly on the page. At first I thought the problem was that I Wasn't using the $(document).ready() handler, but adding it hasn't fixed it. Neither has using an onload handler inside of the body tag.

Any help would be greatly appreciated.

Regards, Ben.

  • 写回答

1条回答 默认 最新

  • dongyi0210 2012-09-29 10:47
    关注

    It's probably due to the fact that your listHotSpots function is called before fetchHotSpots returns (since it's an async call).

    You're better off chaining the execution of listHotSpots to the completion of fetchHotSpots, like so:

    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. Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置