Wednesday 23 September 2020

"Unexpected identifier" error in Javascript when trying to parse JSON with escaped single quote

In my Laravel application, I am passing a JSON object to a Vue component:

<search v-bind:library="''"></search>

language_library(), for testing purposes, returns just one line:

{"you_dont_have_subscription":"You don\\'t have an active subscription right now."}

As you see, the single quote in the string is escaped. However, Vue throws this error:

[Vue warn]: Error compiling template:

invalid expression: Unexpected identifier in

    '{"you_dont_have_subscription":"You don\\'t have an active subscription right now."}'

  Raw expression: v-bind:library="'{"you_dont_have_subscription":"You don\\'t have an active subscription right now."}'"

I don't understand why the issue is happening. It's a single quote inside double quotes and it's escaped too (using addslashes() in the PHP back-end). How can I have a string that contains quotes in my JSON, if not like this?

If, instead of addslahes, I manually do something like this...

str_replace("'", "\'", $string)

...I still get the same error, with two backslashes in front of the quote in JSON.



from "Unexpected identifier" error in Javascript when trying to parse JSON with escaped single quote

No comments:

Post a Comment