weixin_33695450 2015-10-22 03:40 采纳率: 0%
浏览 18

转义Json内容

I am using AJAX function to update a row. When users click on update button, I call an AJAX function which open a div which contains all the form fields. I have rendered a template and passed that as ajax response. However my form contain a textarea inside which html tags does get escaped e.g link would display like

<a href="http://example.com" rel="nofollow" target="_blank">http://example.com/</a>

I have used JSON.stringify, jQuery.parseJSON before populating div and twig's raw function on form filed but no success.

My form field looks like :

{{ form_widget(form.comments,{ 'value': obj.comments|raw}, {'attr':{'style':'width:'}}) }}

I believe, it has nothing to do with JSON as the template is triggered before encoding the content but I wanted to describe the whole scenario.

Controller

$content = $this->renderView("ex.html.twig", array('form'=>$form->createView()));
        return new Response(json_encode($content), 200, array("Content-Type" => "application/json"));
  • 写回答

2条回答 默认 最新

  • local-host 2015-10-22 03:48
    关注

    use JavaScript escape function after using JSON.stringify

    function stringifyP(a) {
        return escape(JSON.stringify(a));
    }
    
    评论

报告相同问题?