[英]Printing json data in javascript function replace quotes by “"”
I'm using Laravel Framework since a couple of weeks now. 从几周以来,我一直在使用Laravel Framework。
I have a strange problem, I would like to convert a PHP 2D array into a JavaScript object, so I used in my controller json_encode() something like : 我有一个奇怪的问题,我想将PHP 2D数组转换为JavaScript对象,因此在控制器json_encode()中使用了类似以下内容:
$arr['ping'] = array_values($arr['ping']);
$arr['doc'] = array_values($arr['doc']);
$data = json_encode($arr, JSON_HEX_QUOT);
Then, in my view file : 然后,在我的视图文件中:
<div class="card-content">
{{ $data }}
</div>
<div id="chartPingListExpand" class="ct-chart"></div>
<script type="text/javascript">
$(document).ready(function() {
app.initChartPingListExpand({{ html_entity_decode($data) }}, {{ $maxping }});
});
</script>
Here is the thing : My {{ $data }}
shows correctly my json string 这是东西:我的{{ $data }}
正确显示了我的json字符串
{"ping":[4,30],"doc":["2018-03-23 09:06:53","2018-03-23 01:04:23"]}
But Javascript doesn't like it : 但是Javascript不喜欢它:
Uncaught SyntaxError: Invalid or unexpected token 未捕获的SyntaxError:无效或意外的令牌
at this line 在这条线
app.initChartPingListExpand({{ html_entity_decode($data) }}, {{ $maxping }});
And I think it's because HTML is translating quotes
by " ;
我认为这是因为HTML会将quotes
翻译为" ;
I've tried many ways to tell the html not to translate those quotes but it does not work, anybody has any idea ? 我尝试了很多方法来告诉html不要翻译那些引号,但是它不起作用,有人有什么主意吗?
Maybe I can pass data to my Javascript function or any other way? 也许我可以将数据传递给Javascript函数或其他任何方式?
Your $data
is already a javascript object, you don't need to decode it 您的$data
已经是一个javascript对象,您无需对其进行解码
You can try if you put var obj = {{ $data }}; console.log(JSON.stringify(obj))
您可以尝试是否将var obj = {{ $data }}; console.log(JSON.stringify(obj))
var obj = {{ $data }}; console.log(JSON.stringify(obj))
Note that Blade will try to protect you and will escape all unsafe characters. 请注意,Blade将尽力保护您,并转义所有不安全的字符。 use 采用
{!! $data !!}
to avoid that 避免那个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.