laravel Js removes special characters such as "escape" in passing json

    Recently, in the laravel framework, the json string passed in the background was obtained from the front-end Blade template, and the " character was found. The phenomenon is as follows:

/*后端*/
$x_char = ["2017-10-25","2017-10-24"];

return view(xxx, ['x_char' => json_encode(x_char)]);

/*前端*/
<script>

var x_char = "{{ $x_char }}";
console.log( x_char ); //输出 [&quot;2017-10-25&quot;,&quot;2017-10-24&quot;]

</script>

Obviously, the json data passed to the Balde template by the backend interface is escaped. And I don't need the escaped json string.

 

There are blog posts on the Internet, and these escape characters are converted back with js regularity, but it is troublesome.

Later, I saw this post, and when I sent the Blade template to receive variables, use {!! $x !!} to prevent $x from being escaped by the php function htmlspecialchars.

Original post: https://segmentfault.com/q/1010000010580668

Laravel official website introduction: https://laravel.com/docs/5.4/blade#displaying-data

 

The modification code is as follows:

/*前端*/
<script>

var x_char = {!! $x_char !!};//将 "{{ $x_char }}" 改为 {!! $x_char !!} 即可
console.log( x_char ); //输出 ["2017-10-25", "2017-10-24"]

</script>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564109&siteId=291194637