【问题标题】:How do I access POST parameters in actions.class.php in symfony 1.4?如何在 symfony 1.4 中访问 actions.class.php 中的 POST 参数?
【发布时间】:2010-07-20 23:17:43
【问题描述】:

我对 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 中无法访问?

【问题讨论】:

    标签: php jquery post symfony-1.4


    【解决方案1】:

    试试这个代码:

    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 控制台查看发送到服务器的数据。

    【讨论】:

    • 谢谢! JavaScript 似乎没有发送有效的 POST 请求。
    • Ajax POST 请求的正确标头是:xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2012-01-01
      • 1970-01-01
      相关资源
      最近更新 更多