1

jQuery 返回链接不起作用。我使用过 jQuery 和基本的 Ajax 功能。我的 jQuery 从文件返回链接Links_ajax.php

我给出了代码示例。

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 获取所有链接。单击它后,我需要一个基本的 Ajax 功能,因为加载了相同的<div> </div>. 但是,即使我完成了,我也无法查看它。我的代码有什么问题吗?还是我必须使用任何其他功能?

4

3 回答 3

1

首先尝试以下方法:

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

这样你就会知道它是否真的在返回数据。

如果这有效,那么您可以更换

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

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

这应该有效。

于 2009-01-01T16:50:59.570 回答
1

我强烈建议使用 FireFox + FireBug 来测试它,或者通过 HTTP 代理记录您的流量的其他浏览器。FireBug 将向您显示来自您的页面的所有 Ajax 请求和响应,并且应该帮助您确定数据是否返回以及返回什么。

于 2009-01-22T21:25:10.663 回答
0

首先,我想知道为什么在已经拥有 jquery 对象的情况下编写自定义函数 HTTPrequest,实际上您已经在顶部使用了 $.ajax,但后来又创建了 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;
            });
       }
});
于 2009-01-28T07:54:12.033 回答