2017-06-02 48 views
0

以下代码位于index.php中。点击时,$选择变量通过ajax传递给ajax.php,后者处理数据并用html代替#div1。在ajax回调中使用ajax回调代替php的div内容?

<script> 
$(document).ready(function(){ 
    $(".header").on("click", function(){ 
     var $selection = 'data to be transmitted'; 
     $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { 'selection' : $selection }, 
      success: function(data) { 
       $("#div1").html(data); 
      } 
     }); 
    }); 
}); 
</script> 

然后,ajax.php使用$ _GET ['selection']处理$选择。然而,在ajax.php年底位于另一个Ajax调用ajax2.php,传递$选择2:

<script> 
$(document).ready(function(){ 
    $(".header").on("click", function(){ 
     var $selection2 = 'depends on $selection from ajax.php'; 
     $.ajax({ 
      type: 'GET', 
      url: 'ajax2.php', 
      data: { 'another_selection' : $selection2 }, 
      success: function(data) { 
       console.log(data); 
       $("#div2").html(data); 
      } 
     }); 
    }); 
}; 
</script> 

两个DIV1和DIV2在index.php文件中找到。我的问题是第二次ajax调用(对ajax2.php)不起作用 - 它不会用从ajax2.php接收到的html代替#div2。

任何想法?我知道被返回的数据是正确的,因为我在控制台中记录了它,并且它是正确的html。我的猜测是“$(”#div2“).html(data);”行不起作用,因为它在ajax.php文件中找到,而不是index.php,其中#div2实际存在。

+1

你知道第二个脚本实际上并不_make_一个AJAX请求,但只有一个元素上注册了一个单击处理? (只有当该点击事件发生时,AJAX请求才会被执行。) – CBroe

+0

为什么不从第一个AJAX调用的'success'函数调用?或者甚至更好,只需在一个请求中返回所需的所有数据 –

回答

0

遇到绑定/常规冲突,尝试这些AJAX调用统一成一个,内回调或外部:

内回调:

<script> 
$(document).ready(function(){ 
    $(".header").on("click", function(){ 
     var $selection = 'data to be transmitted'; 
     $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { 'selection' : $selection }, 
      success: function(data) { 
       $("#div1").html(data); 
       var $selection2 = 'depends on $selection from ajax.php'; 
       $.ajax({ 
       type: 'GET', 
       url: 'ajax2.php', 
       data: { 'another_selection' : $selection2 }, 
       success: function(data) { 
        console.log(data); 
        $("#div2").html(data); 
       } 
       }); 
      } 
     }); 

    }); 
}); 
</script> 

Ouside回调:

<script> 
$(document).ready(function(){ 
    $(".header").on("click", function(){ 
     var $selection = 'data to be transmitted'; 
     $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { 'selection' : $selection }, 
      success: function(data) { 
       $("#div1").html(data); 
      } 
     }); 
      if ($selection == something) 
       var $selection2 = 'depends on $selection from ajax.php'; 
       $.ajax({ 
       type: 'GET', 
       url: 'ajax2.php', 
       data: { 'another_selection' : $selection2 }, 
       success: function(data) { 
        console.log(data); 
        $("#div2").html(data); 
       } 
       }); 

    }); 
}); 
</script> 

但最好的办法是将ajax.php和ajax2.php例程合并为一个,开发一个更简洁的脚本:

<script> 
$(document).ready(function(){ 
    $(".header").on("click", function(){ 
     var $selection = 'data to be transmitted'; 
     if ($selection == something) 
      var $selection2 = 'selection2 data/condition that depends selection'; 
     $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { 
       'selection': $selection, 
       'anotherselection': $selection2   
       }, 
      success: function(data) { 
       $("#div1").html(data); 
       $("#div2").html(data); 
      } 
     }); 

    }); 
}); 
</script> 

到PHP:

<?php 
if (isset($_GET['selection'])){ 
    //first ajax routine 
} 

if (isset($_GET['anotherselection'])){ 
    //second ajax routine (ajax2.php) 
}