【问题标题】:AJAX posting to CakePHP controller actionAJAX 发布到 CakePHP 控制器动作
【发布时间】:2012-04-23 13:40:15
【问题描述】:

我将一些用于注册的表单值发布到 cakePHP 控制器操作中。代码是这样的

var params='firstname='+encodeURIComponent(firstname) +'&lastname='+encodeURIComponent(lastname)+'&emailid='+encodeURIComponent(emailid)+'&password='+encodeURIComponent(password);

xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);

这是在发帖,我可以在 POST 部分源的 Firebug 中看到它

firstname=name&lastname=name&emailid=mymailid&password=123123

但是当我在 action 中打印 $this->data 时,它没有显示任何值。我什至用过 $_POST 它也返回任何东西..

我在这里做错了什么..?

【问题讨论】:

    标签: ajax cakephp cakephp-1.3


    【解决方案1】:

    在控制器中尝试如下:

    $this->params['url']; // this will give an array of parameters
    

    或者,$this->request['url']; // this will give an array of parameters

    【讨论】:

      【解决方案2】:

      您缺少使用 POST 发送变量时所需的请求标头:

      试试这个:

      var params='firstname='+encodeURIComponent(firstname) +'&lastname='+encodeURIComponent(lastname)+'&emailid='+encodeURIComponent(emailid)+'&password='+encodeURIComponent(password);
      
      xmlhttp.open('POST', url, true);
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.send(params);
      

      【讨论】:

      • 我已经添加并使用了 $this->params['url'];和 $this->request['url'];。第一个只显示我要发布到的 URL,第二个什么都不显示。
      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2013-08-10
      • 2013-05-14
      • 1970-01-01
      • 2013-08-27
      • 2013-02-27
      • 1970-01-01
      相关资源
      最近更新 更多