【问题标题】:Cakephp 2.0 Ajax post data not showing in the controller?Cakephp 2.0 Ajax 发布数据未显示在控制器中?
【发布时间】:2016-08-01 14:46:09
【问题描述】:

我正在使用 cakephp Web 应用程序,我在 default ctp file 中创建了一个小的 Ajax function。设置文件现在我想将值数据发送到另一个控制器函数,在 chrome browser network 中显示 Ajax 发布到所需的 url 但在控制器中当我尝试回显 $_post ['selectedlocation'] 时未显示值??

Ajax 默认文件

var DisplayLocationName = $('#ListingLocation option:selected').val();

$.ajax({
     type: "POST",
     url: '/listings/home',
     data: {selectedlocation:DisplayLocationName },
     dataType:"text",
     success: function(result) {

     }
     });

列出控制器功能

function home()
{

    if(!isset($this->params['requested']) || $this->params['requested'] != true)
    {
        $this->actionId = 'home';
    }

    $this->viewFile = null;

    if($this->params['isAjax'] &&  isset($_POST['selectedlocation'])) 
    {

            echo "erreerreer";
            echo $selectloc= $_POST['selectedlocation'];
            $this->set('selectloc',$selectloc); 

    }

}

我如何在默认情况下显示 Ajax 发布值以进行测试,以确保 Ajax 向控制器发布正确的值

【问题讨论】:

    标签: ajax cakephp controller default


    【解决方案1】:

    您将需要使用日志文件来读取发布数据。这会将数据写入错误日志,您可以在那里查看。

    function home()
    {
    
        if(!isset($this->params['requested']) || $this->params['requested'] != true)
        {
            $this->actionId = 'home';
        }
    
        $this->viewFile = null;
    
        if($this->params['isAjax'] &&  isset($_POST['selectedlocation'])) 
        {
    
                echo "erreerreer";
                CakeLog::write('error', $this->request->data('selectedlocation));
                echo $selectloc= $_POST['selectedlocation'];
                $this->set('selectloc',$selectloc); 
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      当你想 $_POST 来自 ajax 请求的数据时,你应该使用:

      $form_data = $this->request->data; (cakephp 2.x)
      $form_data = $this->params['form'] (cakephp 1.3)
      

      现在你得到了一个包含 $_POST 数据的数组,如果你愿意,可以尝试 var_dump() 来了解它的结构。

      【讨论】:

      • 如果我不使用表单比?
      • 尝试:$this->data['selectedlocation'];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多