简体   繁体   中英

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.

The string is like [{"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.

It seems that the string is passed into the browser as it should be. But Chrome converts it.

How can I solve this? I've tried PHP's str_replace() , html_entity_decode() , json_encode() and Javascript's replace() . Both aren't solving the issue.

I think you are using blade template in laravel. Could you please try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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