简体   繁体   中英

laravel json result replace “/” with “\/”

Hi was wondering when I return JSON result using laravel 5 it replace every single '/' with '\\/' which cause me a trouble as the url I return through the controller is no longer working

for example, in controller:

$url = 'icon/nature/animals/cat-2.png';
$result = array('data'=>$url);

return response()->json($result);

in jquery response it returns 'icons\\\\/nature\\\\/animals\\\\/cat-2.png'

How can I avoid this to happen, thank you

you have to use encodeURI (build-in javascript function):

For example:

encodeURI('http:\/\/www.google.com') => 'http://www.google.com'

encodeURI('icons\/nature\/animals\/cat-2.png') => 'icons/nature/animals/cat-2.png'

This is the json() method's blueprint:

public function json($data = [], $status = 200, array $headers = [], $options = 0)

Use the $options parameter to set your desired escape options. To see a full list of JSON constants, check this link: http://php.net/manual/en/json.constants.php

You are going to need the JSON_UNESCAPED_SLASHES constant to achieve the behavior you need.

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