douyunjiaok300404 2017-11-07 22:01
浏览 117
已采纳

PHP使用json stringify对象[关闭]

I have an object in the frontend and need to pass this value to the backend (PHP).

Frontend:

var cfg = {"tooltips":true,"tooltipTemplate":"<div></div>"},
jsonCfg = JSON.stringify(cfg);
$inputConfig.val(jsonCfg);
$form.submit();

Backend:

$config = $_POST['config'];
$json = json_decode($config);
echo $json->tooltips;
echo $json->tooltipTemplate;

The last string gives me only empty value, if I remove the first tag "<", I will get only "div>", but I do not understand how to prevent this behavior, I need any string without filtering or formatting.

  • 写回答

1条回答 默认 最新

  • dth62818 2017-11-07 22:05
    关注

    Your problem is simply, that <div></div> isn't visible in the browser. If you want to view the correct output, have a look at the page source or use htmlentities to escape the html tags.

    $config = $_POST['config'];
    $json = json_decode($config);
    echo $json->tooltips;
    echo htmlentities($json->tooltipTemplate);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?