【问题标题】:Construct JSON object with symfony 1.4, with a field with html content用 symfony 1.4 构造 JSON 对象,带有 html 内容的字段
【发布时间】:2012-01-05 17:30:54
【问题描述】:

我在这里看到了一些关于几乎相同的问题的问题,但是,在遵循他们的建议后,我一直得到相同的行为。

我正在按如下方式在我的操作中构造 json 对象:

return $this->renderText(json_encode(array(
            'html'  => addslashes($this->renderPartial('global/formWrapper',
                    array(
                        'form'  => $nareas,
                        'url'   => $this->generateUrl('new_promo_step2'),
                        'cancelUrl' => 'new_promo',
                          ))),
            'error' => true)));

我得到以下回复:

<html>
  The html content which is supposed to be in the "html" property of my json
  object...
</html>{"html":"None","error":true}

以及带有Content-Type:text/html; charset=utf-8 的标题。

在我的 jQuery ajax 请求中,我将 dataType 设置为“json”(当然,我会遇到意外的字符错误)和“text”(我没有收到任何错误,但会不断收到格式错误的 json)

我尝试过使用addslashes()(如示例中所示),但没有此功能;尝试使用ESC_RAW symfony 选项,结果相同。

我希望得到这样的东西:

{"html":"<html>Html content</html>","error":true}

请!任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: php jquery symfony-1.4 json


    【解决方案1】:

    要从操作中渲染部分内容,您需要将部分内容作为包含加载并使用 getPartial 将其检索到您的 renderText 调用中。

    sfLoader::loadHelpers('Partial');
    return $this->renderText(json_encode(array(
            'html'  => addslashes(getPartial('global/formWrapper',
                    array(
                        'form'  => $nareas,
                        'url'   => $this->generateUrl('new_promo_step2'),
                        'cancelUrl' => 'new_promo',
                          ))),
            'error' => true)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多