【问题标题】:CodeIgniter/jQuery - Ajax call returns full html page instead of my echoCodeIgniter/jQuery - Ajax 调用返回完整的 html 页面而不是我的回声
【发布时间】:2011-08-03 23:57:37
【问题描述】:

在我看来,我有一个 ajax 调用:

    $(".previous").click(function() {
        $.ajax({
            type: "POST",
            url: "planner/get_cal",
            data: {current_month: current_month},
            success: function(msg){
                alert(msg);
            }

        });

我的 Planner 控制器中的 get_cal 函数:

function get_cal()
{
    echo "dinosaurs";
}

但是,它不是返回“恐龙”,而是返回一个完整的 HTML 页面。我不知道为什么。想法?非常感谢。

【问题讨论】:

  • 你有绑定到你的控制器的布局来渲染它吗?
  • 从浏览器手动尝试会得到什么?
  • @krishna,手动转到 get_cal 页面会得到一个空白页面,其中包含“恐龙”,正如预期的那样。
  • 您是否尝试过添加前导斜杠? url: "/planner/get_cal",
  • 我第二个领先的 / -- 否则事情看起来还不错,这不应该发生。

标签: php jquery ajax codeigniter


【解决方案1】:

我按照我的问题的 cmets 建议使用前导斜杠解决了这个问题。

$.ajax({
  type: "POST",
  url: "/planner/get_cal",
  dataType: "text",
  data: {current_month: current_month},
  success: function(msg){
    alert(msg);
  } 
});         

【讨论】:

    【解决方案2】:

    您也可以通过在您的 php 文件中的 echo 后添加 exit 来获取它,如下所示:

    function get_cal()
     {
        echo "dinosaurs";exit;
    }
    

    它会起作用的。 :)

    【讨论】:

    • 现在有效。不过,我将exit 更改为return,:)
    【解决方案3】:

    尝试将数据类型设置为“文本”

    
    $.ajax({
      type: "POST",
      url: "planner/get_cal",
      data: {current_month: current_month},
      dataType: "text",
      success: function(msg){
          alert(msg);
      }
    });
    

    【讨论】:

      【解决方案4】:

      当使用 Controler/Method uri 段的 CodeIgniter 结构时,我发现在 jquery ajax 请求中使用 ../../controller/method 作为我的 URL 要容易得多。我还建议指定一个数据类型,以便将字符串解析并作为对象返回。

      这是一个例子;

      
      $.ajax({  
           type: "POST",  
           dataType: "json",  
           url: "../../controller/method",  
           success: mySuccessFunction  
      }); 
      

      【讨论】:

        【解决方案5】:

        当您的 php 文件和 html 文件不在正确的路径上时,就会出现这些类型的问题,以便 apache 服务器可以解析 php 文件。 没有提及 type:'text' 和任何其他格式,ajax 也可以工作。 但请确保您的服务器正在访问 php 文件。否则整个文件将被视为文本并返回。

        【讨论】:

          【解决方案6】:

          对于使用 Zend 框架的任何人,我都遇到了同样的问题,即 AJAX 响应返回完整的 HTML 而不是 json_encode() 响应。这已通过将以下内容添加到我的控制器来解决:

              if ($this->getRequest()->isXmlHttpRequest())
              {
                  $this->_helper->layout()->disableLayout();
                  $this->_helper->viewRenderer->setNoRender(true);
              }
          

          【讨论】:

            【解决方案7】:

            只是想提一下: url 将真正取决于您如何设置 .htaccess 和文件夹结构。 所以最好的方法是尝试几个网址,即:
            ../../控制器/方法
            ../控制器/方法
            server_folder/index.php/controller/method
            http://example.com/server_folder/index.php/controller/method
            然后选择在给定情况下效果最好的那个。

            【讨论】:

              【解决方案8】:

              // 主页视图

              $("#button").click(function(e)
              {
                value=$("#input_value").val();
              
                $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>path",
                data: "type=get_path_details&mobile="+value,
                cache: true,
                dataType:"html",
                async: false,
                success: function(data)
                {
                $('.apend_new_div').empty().append(data);
                $('.scroll_card').css('overflow-x','scroll');
                }
                });
              
              });
              

              // 控制器

              public function path()
              {   
                $id = $this->input->post("mobile");
                // Some function goes here
                // $template[''] = ;
                $this->load->view('ajax_page',$template);
              }
              

              // ajax 视图页面

              <?php if($_POST['type']=='get_path_details'){if(!empty($template)){ ?>
              // Html code 
              <?php }} ?>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-01-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-02
                • 2013-07-19
                • 2015-03-25
                • 2015-07-15
                相关资源
                最近更新 更多