php - AJAX/jQueryalert() 有效,.html() 或 .text() 无效

标签 php jquery ajax

我是 jquery 和 AJAX 的新手,并且在将 AJAX 响应简单地返回到 div 中时遇到了一个非常烦人的问题。

代码

$(document).ready(function() {
    $.ajax({
        method: "GET",
        url: "lists2.php",
        cache: false,
        dataType: "html",
        beforeSend: function() {
            $("#sidebar-content").text("Loading..");
        },
        complete: function() {
            $("#sidebar-content").text("");
        },
        success: function(lists) {
            $("#sidebar-content").text(lists);
        },
        error: function() {
            alert("Something went wrong..");
        }
    });
});

代码正常触发,没有出现错误,Firebug 也没有显示错误,如果我输入 例如 alert(lists) 而不是 .text().html() 它可以工作并警告数据。但是 .text().html() 不起作用。

lists2.php 是一个复杂的文件,它分析来自 SQL db 的数据并在引导侧边栏中输出内容。在没有 AJAX 的旧版本中它工作得很好。

我做错了什么?

最佳答案

Complete()success() 之后触发,并清除您的内容。

Complete

A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

http://api.jquery.com/jquery.ajax/

关于php - AJAX/jQueryalert() 有效,.html() 或 .text() 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384474/

相关文章:

php - 将敏感数据安全地存储在数据库中

javascript - window.resize 不在滚动

java - Spring MVC @RequestBody 不适用于 jquery ajax?

javascript - 我可以将变量附加到 .load 方法中的 url 并加载到文档的特定部分吗?

Javascript 或 Jquery 检测页面刷新并重定向到错误页面 - 避免重复数据提交

php - 在谷歌文档查看器中显示 AWS 文件

php - pdo 免费结果

php - 匹配utf中的整个单词

javascript - 使用 jQuery fadeIn 还是 CSS3 动画更好?

javascript - DreamFactory REST API POST rest/user/session 请求在 IE9 中总是返回错误