0

我刚刚开始了解 AJAX。为此,我正在尝试在本地计算机上搜索时找到的示例。但它不起作用。

页面加载时页面有一些静态文本,一旦我们向下滚动,使用 ajax 添加新的动态文本,但在滚动时不会添加新文本。

html文件代码为:

 <script type="text/javascript" language="javascript">
    $(document).ready(function () {

        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                sendData();
            }
        });

        function sendData() {
            $.ajax(
             {
                 type: "POST",
                 url: "https://localhost/kailash/cgi/testing/getdata.pl",
                 data: "{}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: "true",
                 cache: "false",

                 success: function (msg) {
                     $("#myDiv").append(msg.d);
                 },

                 Error: function (x, e) {
                     alert("Some error");
                 }

             });

        }

    });


</script>
</head>
<body>
<form id="form1" runat="server">
<div id="myDiv">

    <p>
        Static data initially rendered.
    </p>

它调用 getdata.pl。getdata.pl 中的代码是

our $resp;
my $cgi = new CGI;
print $cgi->header();

$resp = "<p>This content is dynamically appended to the existing content on scrolling.</p>";
return "$resp\n";

所以这是行不通的。你能帮我让它工作吗?请让我知道其中缺少什么。

4

3 回答 3

0

一种可能性:将您的绝对https:URL 更改为相对 URL(例如/kailash/cgi/testing/getdata.pl)。通过 AJAX 连接到不同域的唯一方法是使用JSONP

另一种可能性:您的 AJAX 调用从未运行。在成功调用中添加一个alert()以确保sendData()实际调用它。

这是如何正确使用的一个示例$.ajax

于 2012-07-18T13:36:12.213 回答
0

你确定你的
$("#myDiv").append(msg.d);

,你试过 $("#myDiv").append(msg. responseText); ?

于 2012-07-18T13:40:44.830 回答
0

我已经解决了这个错误,由于这篇文章的帮助,我的代码中出现了错误: 检查这个

我取出数据类型字段并仅使用:

$("#myDiv").append(msg);

现在我正在获取 ajax 内容。AJAX 真的很酷,当我向下滚动时会添加文本:)。

于 2012-07-18T14:24:46.723 回答