【问题标题】:How to read the ajax data from a web pages in cakephp controller using $this->request->data('Name.of.the Key')如何使用 $this->request->data('Name.of.the Key') 从 cakephp 控制器中的网页读取 ajax 数据
【发布时间】:2012-08-24 06:29:36
【问题描述】:

这是 email_list 管理控制器页面中的代码:

$idOfCourse = $this->request->data('courseId'); /* 在这两行中,我正在读取从 Js 页面发布的数据*/ $emailArray = $this->request->data('emailList');

这是对应JS中的ajax代码 `var 数据 = { courseId : id, 电子邮件列表:myArray }; //这里我用 2 个字段 courseID 和 emailList 格式化 JSON 格式

  $.ajax({  //here I am giving the call
     type:"POST",
     url :"/adata/admin/email_list",
     data: { studentcoursemaildata : data, action : "/adata/admin/template1"}
   }).done(function(){
     window.location.href ="/admin/email/";  /*I am redirecting after the data is being   fetched */
   });

`

【问题讨论】:

  • 检查控制器中debug($this->request->data);` 的输出。

标签: jquery cakephp-2.1


【解决方案1】:

正确解决方案的关键是充分了解如何访问 JSON 格式的数据。

首先我们必须读取 studentcourseemaildata,因为它包含整个数据。

在控制器中会这样读取-:

$someVariable = $this->request->data('studentcourseemaildata');在适当的控制器中执行此操作后,我们得到一个数组。从中读取相应的组成字段很简单,可以这样完成:

idOfTheCourse = $someVariable('courseId'); $mailArray = $someVariable['emailList'];

就是这样。

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多