2015-12-30 115 views
-1

我正在使用AJAX請求來獲取從數據庫中獲取的一些數據。我已經在HTML頁面中包含了四個.js文件。他們是當HTML頁面中包含其他JS腳本時,Ajax請求不起作用

<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

+3

究竟是什麼問題?它是否生成錯誤,是否沒有Ajax請求,請求是否返回錯誤?那些2個JS文件裏面有什麼? – Thomas

+0

是否存在js衝突問題? –

+4

嘗試在此ajax腳本之後放置腳本。 – Manikiran

回答

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的任何其他框架。

+0

謝謝。有效。但是,如何通過點擊事件來做到這一點。嘗試以下但沒有工作 – Gaurav

+0

(功能($){ $(「#category」)。click(function(){ $ .ajax({//創建一個ajax請求到load_page.php 類型: 「POST」, 網址: 「PHP/fetch_candidates.php」, 異步:真實, 數據類型: 「HTML」,//預期的HTML返回 錯誤:函數(){ 迴歸真實; }, 成功:function(response){(「#candidates_list」)。html(response); // alert(response); } }); }); })( jQuery的); – Gaurav

+0

@GauravBora控制檯上發生了什麼?任何錯誤? AJAX事件至少發射了嗎? –

0

嘗試使用var jq = $.noConflict();,只是$(".buttonid").click(

前和使用jq而不是$,它看起來像一個衝突的問題,可以請你分享什麼是framework.plugins.jscustom.js

+0

Hi Satpal,請問我爲什麼編輯答案,刪除了我的角色並添加了相同的字符? – robindersingh1986

+1

你沒有看到他修復了代碼格式嗎? –

+0

@ robindersingh1986難道你沒有看到你的答案前後看起來如何? –