简体   繁体   English

将带有引号的字符串从PHP插入javascript

[英]Inject a string with quotes from PHP to javascript

I'm facing a problem when trying to inject a string from PHP (using the Laravel framework) into javascript. 尝试从PHP(使用Laravel框架)将字符串注入javascript时遇到问题。

The string is like [{"foo1": bar1}, {"foo2" : bar2}] . 字符串就像[{"foo1": bar1}, {"foo2" : bar2}] When I inject this string to javascript using something like var test = '{{ $var }}' the " are converted to " . The javascript string is used in a function later to parse the JSON. This now fails because of the quote-conversion. 当我使用类似var test = '{{ $var }}'之类的东西将字符串注入javascript时, "转换为" 。稍后在函数中使用javascript字符串来解析JSON。由于引号,现在此操作失败γ变换。

It seems that the string is passed into the browser as it should be. 似乎该字符串已正确传递到浏览器中。 But Chrome converts it. 但是Chrome会对其进行转换。

How can I solve this? 我该如何解决? I've tried PHP's str_replace() , html_entity_decode() , json_encode() and Javascript's replace() . 我已经尝试过PHP的str_replace()html_entity_decode()json_encode()和Javascript的replace() Both aren't solving the issue. 两者都没有解决问题。

I think you are using blade template in laravel. 我认为您在laravel中使用刀片模板。 Could you please try 你能试一下吗

{ $var } instead of {{ $var }} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM