2008-12-18 56 views
0

jQuery返回鏈接不起作用。我已經使用jQuery和基本的Ajax功能。我的jQuery返回文件Links_ajax.php的鏈接。jQuery Ajax返回鏈接不工作

我給的代碼樣本。

GetCustomerData.php有:

<html> 
    <script type="text/javascript"> 
    <script src="ajax.js" type="text/javascript"> 

     $(function() { 
      ..... 

      $.ajax({ 
       type: 'POST', 
       url: 'Links_ajax.php', 
       data: 'category='+category , 
       success: function(data){ 
        $("#response").html(data); 
       } 
      }); 
     } 
     ...... 
    } 

    ... 

    <! Links return here 
     <div id="response"> 
     </div> 
</html> 

<? 
    echo "Some Code"; 


?> 

    .... 

我Links_ajax.php具有下面的代碼

.... 

echo '<a href="getCustomerData.php?id=10" onclick="requestCustomerInfo();return false;">show me </a>'; 
echo '<a href="getCustomerData.php?id=11" onclick="requestCustomerInfo();return false;">show me </a>'; 

.... 

現在我來給主頁getCustomerData.php。我使用了從Links_ajax.php返回鏈接的Ajax功能。

..... 

所以,我的ajax.js有以下的Ajax腳本。

var url = "GetCustomerData.php?id="; // The server-side script 
function handleHttpResponse() { 
    if (http.readyState == 4) { 
     if (http.status == 200) { 
      var results = http.responseText; 
      document.getElementById('divCustomerInfo').innerHTML = results; 
     } 
    } 
} 

function requestCustomerInfo(event) { 
    if(event &&event.preventDefault) event.preventDefault(); 
    else if (window.event && window.event.returnValue) 
     window.eventReturnValue = false; 
     var sId = 10; 
     http.open("GET", url + escape(sId), true); 
     http.onreadystatechange = handleHttpResponse; 
     http.send(null); 
    } 

    function getHTTPObject() { 
     var xmlhttp; 

     if (window.XMLHttpRequest){ 
      xmlhttp = new XMLHttpRequest(); 
     } 
     else if (window.ActiveXObject){ 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      if (!xmlhttp){ 
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
     } 
     return xmlhttp; 
    } 

    var http = getHTTPObject(); // We create the HTTP object 

......

現在我來的問題。當我執行GetCustomerdata.php時,我能夠從ajax.JQuery獲取所有鏈接。 而在點擊,我需要加載以來同<div></div>一個基本的Ajax特性。但即使完成,我也無法查看它。 我的代碼有什麼問題嗎?或者我必須使用任何其他功能?

回答

-1

首先我不知道爲什麼你寫了一個自定義函數的HttpRequest,而你已經擁有jQuery對象,事實上你已經在使用$就在頂部,但隨後()後創建getHTTPObject。

的問題是JavaScript函數requestCustomerInfo()從未授權以處理Ajax響應數據的鏈接,你需要委派功能作爲Ajax請求的回調

$.ajax({ 
     type:'POST', 
     url: 'Links_ajax.php', 
     dataType: 'html', 
     data:'category='+category, 
     success: function(data){ 
      $("#response").html(data); 
      // here add the link click handler 
      $("#response a").click(function(){ 
       // make http get request & custom handler 
       $.get('GetCustomerData.php?id=10',function(result){ 
        $('#divCustomerInfo').html(result); 
       }); return false; 
      }); 
     } 
}); 
-1

對於初學者來說,不要窩腳本標記。帶有源代碼的腳本標籤應該立即關閉,並且應該位於或之下而不在其他位置。

0

首先嚐試以下操作:

success: function(data){ 
    //$("#response").html(data); 
    alert(data); 
    } 

這樣你就會知道,如果它真的返回數據或不。

如果這是工作,那麼你可以用

document.getElementById('response').innerHTML = data; 

這應該更換

`$("#response").html(data)` 

0

我強烈建議,要麼使用的是Firefox +螢火測試,或通過將記錄您的流量HTTP代理一些其他的瀏覽器。螢火蟲會告訴你所有的Ajax請求的並迴應從您的網頁,並應幫助您確定數據是否回來了,和什麼。