【问题标题】:.ajax() "GET" within an .ajax() "GET" , ajax call now showing correctly.ajax() "GET" 中的 .ajax() "GET" ,现在可以正确显示 ajax 调用
【发布时间】:2012-04-28 02:57:45
【问题描述】:

我有两个 ajax 调用,但由于某种原因,其中一个 ajax 没有按计划工作。

一个 ajax 调用正在从站点 ajax1.php 获取数据,然后另一个 ajax 正在从 ajax2.php 获取数据,并且它应该更改 ajax1.php 中的一些数据。

所以我有index.php,而 ajax 是这样的

这是index.php

    $.ajax({
        type: "GET",
        data: "id="+id+"&id-other="+id-other,
        url: "ajax1.php"
    }).done(function(data){
        $("#div").html(data);

    });

    $.ajax({
        type: "GET",
        data: "id_1="+id+"&id_2="+id_2,
        url:"ajax/ajax2.php"
    }).done(function(data){
        $("#change_data").html(data);

    });

<div id="div">
<div id="change_data"><!-- This div is supposed to be in ajax1.php so it only appears after the first ajax is done.!--></div>
</div>

问题是#change_data 没有正确显示。它会在一秒钟内显示它应该显示的内容,然后消失。不确定是什么问题。我尝试延迟:$("#change_data").delay.(1000).html(data_changed); 就像这样,但不起作用。我尝试将整个 ajax 函数放在第一个 .done() ajax 函数中,但这不起作用。

这可能是因为当第一个 ajax 加载时,第二个 ajax 尝试比第一个 ajax 加载得更快。第一个 ajax 包含更多信息,因此可能需要加载更长时间?

有什么想法吗?谢谢 也许还有另一种方法可以将第二个 ajax 放在第一个等成功的情况下,它会调用第二个 ajax?

【问题讨论】:

    标签: php jquery ajax get


    【解决方案1】:

    也许还有另一种方法可以将第二个 ajax 放在第一个中,如果成功,它会调用第二个 ajax?

    是的,有:

    $.ajax({
        type: "GET",
        data: "id="+id+"&id-other="+id-other,
        url: "ajax1.php"
    }).done(function(data){
        $("#div").html(data);
        $.ajax({
            type: "GET",
            data: "id_1="+id+"&id_2="+id_2,
            url:"ajax/ajax2.php"
        }).done(function(data){
            $("#change_data").html(data);
        });
    });
    

    【讨论】:

    • @drew010,看来我不是! :( 我刚刚在他的代码上看到了这条评论:&lt;!-- This div is supposed to be in ajax1.php so it only appears after the first ajax is done.!--&gt;
    • @bfavaretto,是的,#change_data 在ajax1.php,我想更改其中的数据
    【解决方案2】:

    $.ajax 调用是异步的,所以第一次调用不能保证在第二次调用发生时完成。您应该在第一次调用的回调中进行第二次调用,例如:

    $.ajax({
            type: "GET",
            data: "id="+id+"&id-other="+id-other,
            url: "ajax1.php"
        }).done(function(data){
            $("#div").html(data);
    
    
            $.ajax({
               type: "GET",
               data: "id_1="+id+"&id_2="+id_2,
               url:"ajax/ajax2.php"
            }).done(function(data){
               $("#change_data").html(data);
            });
    
    });
    

    【讨论】:

    • 是的,我想,但我没有经验知道我可以用这种方式做什么?我只是尝试将整个 ajax 函数放在第一个 ajax 函数中,但是第二个 ajax 没有调用?它应该可以工作吗,我可能只需要修复一些其他的东西吗?
    • 我刚刚编辑了我的回复 - 它应该可以工作,我在自己的代码中的某些地方在 ajax 调用中使用了这样的 ajax 调用
    猜你喜欢
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多