当其他JS脚本包含在html页面中时,Ajax请求不起作用

时间:2015-12-30 10:59:43

标签: javascript php jquery html ajax

我正在使用AJAX请求从数据库中获取一些数据。我在HTML页面中包含了四个.js个文件。他们是

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jqueryui.js"></script>
<script type="text/javascript" src="scripts/framework.plugins.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $(".buttonid").click(
    function(){
          $.ajax({    //create an ajax request to load_page.php
            type: "POST",
            url: "php/fetch_candidates.php",
            async: true,             
            dataType: "html",   //expect html to be returned   
            error: function(){
                    return true;
            },                       
            success: function(response){                    
                $("#responsecontainer").html(response); 
                //alert(response);
            }
    });

    });
});
</script>

这不起作用。但如果我删除

<script type="text/javascript" src="scripts/framework.plugins.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>
然后AJAX开始工作。我如何解决这个问题,因为我需要将所有.js文件保存在html页面中。

有一个responsecontainer div,它在ajax请求后处理html。问题只在于包含那两个js脚本。 TIA

2 个答案:

答案 0 :(得分:0)

尝试在var jq = $.noConflict();

之前使用$(".buttonid").click(

并使用jq代替$,这看起来像是一个冲突问题,请您分享framework.plugins.jscustom.js

内的内容

答案 1 :(得分:0)

当其他框架也使用$函数时,它可能会覆盖jQuery的$功能行为。但这会使jQuery变量保持不变。所以尝试使用闭包并在IIFE中执行它:

(function ($) {
  $(document).ready(function() {
    $(".buttonid").click(function () {
      $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "php/fetch_candidates.php",
        async: true,             
        dataType: "html",   //expect html to be returned   
        error: function(){
          return true;
        },                       
        success: function(response){                    
          $("#responsecontainer").html(response); 
          //alert(response);
        }
      });
    });
  });
})(jQuery);

这会将所有$转换为jQuery的原始函数,并确保仅使用$仅使用jQuery而不使用PrototypeJS或Scriptaculous等任何其他框架。< / p>

相关问题