Laravel json结果替换"/"与“ /“


laravel json result replace "/" with "/"

我想知道当我使用laravel 5返回JSON结果时,它会用''/'替换每一个'/',这会给我带来麻烦,因为我通过控制器返回的url不再工作

例如,在controller:

$url = 'icon/nature/animals/cat-2.png';
$result = array('data'=>$url);
return response()->json($result);
在jquery响应中,它返回'icons''/nature''/animals''/cat-2.png'

我怎样才能避免这种情况的发生,谢谢

你必须使用encodeURI(内置javascript函数):

例如:

encodeURI('http:'/'/www.google.com') => 'http://www.google.com'
encodeURI('icons'/nature'/animals'/cat-2.png') => 'icons/nature/animals/cat-2.png'

这是json()方法的蓝图:

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

使用$options参数设置所需的转义选项。要查看JSON常量的完整列表,请查看此链接:http://php.net/manual/en/json.constants.php

您将需要JSON_UNESCAPED_SLASHES常量来实现您需要的行为。