PHP AJAX 调用不检索任何内容


PHP AJAX call not retrieving anything

我正在尝试检索 HTML 并将其显示在页面上,它似乎没有检索到任何东西......

我的控制器脚本:

function index()
{   
    $this->load->view('ajax_post');
}
public function ajaxify()
{
    $message = "<b>hello</b>";
    echo $message;
}

我的JavaScript代码:

  $(document).ready(function(){
    $("#click").click( 
    function(){
        $.ajax({
        type: "POST",
        url: "/index.php/ajax_post/ajaxify",
        dataType: "html",
        cache:false,
        success: 
          function(data){
            $("#content").append(html); 
          }
        });
      return false;
    });

});

任何帮助将不胜感激,因为我是jQuery/AJAX的绝对初学者

谢谢

您的 URL 构建错误;如果您在 Firebug 中看到 NET 控制台,它将显示 404(或 500(。使用 CI 中的帮助程序函数(url 帮助程序(:

$.ajax({
    type: "POST",
    url: "<?php echo site_url('ajax_post/ajaxify');?>",
    dataType: "html",

或手动构建它,包括域名:

$.ajax({
    type: "POST",
    url: "http://www.yourdomain.com/index.php/ajax_post/ajaxify",
    dataType: "html",

确保 url 映射到正确的控制器/方法(ajax_post.php控制器,如果未分配自定义路由,则为 ajaxify 方法(

旁注,append()附加到所选元素的末尾,如果要插入元素内部,请使用 $(selector).html(data);

你的 Ajax 成功处理程序函数:

function(data){
    $("#content").append(html); 
}

有一个名为 data 的参数,但随后您尝试追加一个不存在的变量html请尝试将其更改为:

    $("#content").append(data);