1

我对 symfony 还是很陌生,所以如果这是一个愚蠢的问题,我很抱歉。我将 symfony 1.4 与 Doctrine 一起使用。我的同事编写了一个 JavaScript 来从我们的客户端小部件向我们的服务器生成报告:

$j.post(serverpath, {widget_id:widget_id, user_id:user_id, object_id:object_id, action_type:action_type, text_value:stuff_to_report });

我在 routing.yml 中创建了一个路由来接收这个请求:

widget_report:
  url: /widget/report/
  options: {model: ReportClass, type: object }
  param: {module: widget, action: reports}
  requirements:
    object_id: \d+
    user_id: \d+
    action_type: \d+
    sf_method: [post]

我在 actions.class.php 中创建了一个动作来处理请求:

  public function executeReports(sfWebRequest $request) {
    foreach($request->getParameterHolder()->getAll() as $param => $val) {
        // $param is the query string name, $val is its value
        $this->logMessage("executeReports: $param is $val");  
    }
    try {
      [...]
     $actionHistory->setUserId($request->getParameter('user_id', 1));
     $this->logMessage("executeReports success: ");  
    } catch {
      [...]
    }
  }

我的日志文件报告:

Jul 20 18:51:35 symfony [info] {widgetActions} Call "widgetActions->executeReports()"
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: module is widget
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: action is reports
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports success: 

我一定在这里错过了一步。在传递 URL 中的变量(当然,在路由中指定变量)时,我们已经完成了这项工作,但由于各种原因,我们希望使用 POST 来代替。

为什么我的 POST 参数无法在 actions.class.php 中访问?

4

2 回答 2

3

试试这个代码:

if ($request->isMethod('post')) {
    foreach($request->getPostParameters() as $param => $val) {
        $this->logMessage("executeReports: $param is $val");
    }
} else {
    $this->logMessage("executeReports: request method is not POST");
}

如果这没有帮助,请尝试:

$this->logMessage("executeReports: " . var_export($_POST, true));

或者启用 symfony 调试工具栏并查看 POST 变量是否来自浏览器。

如果 $_POST 数组为空,则问题可能出在错误的请求标头中,要检查这一点,请尝试以下操作:

$fp = fopen('php://input','r');
$this->logMessage("executeReports: " . stream_get_contents($fp));

祝你好运!

编辑:

也许你可以在这里找到你的答案$.post not POSTing anything

例如,您应该检查所有 JS 变量是否不为空。

无论哪种情况,我都建议您使用 Firebug 控制台查看发送到服务器的数据。

于 2010-07-21T01:57:52.023 回答
1

作为对 Sergiy 的回应,根据 jQuery 文档 ( http://api.jquery.com/jQuery.ajax ),任何 jQuery ajax 请求的默认设置都是 application/x-www-form-urlencoded 和 UTF-8。$.post 只是一个快捷方式,它将 ajax 'type' 设置为 'POST'。

是否可能是大小写不匹配,即 POST 与 post?

于 2010-07-21T15:08:53.447 回答