2016-07-02 32 views
0

您好想知道當我使用laravel 5返回JSON結果時,它將替換每個'/''\/',這使我很麻煩,因爲通過控制器返回的url不再有效laravel json結果用「 /」替換「/」

例如,在控制器:

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

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

jQuery的響應返回'icons\\/nature\\/animals\\/cat-2.png'

我如何才能避免這種情況發生,謝謝

+0

這是一個錯誤,或者大家都知道,它應該是: '$結果=陣列( '數據'=> $ iklans);' 但也許?: '$結果=陣列( '數據'=> $ '' – W92

+0

@ W92不,我的代碼很糟糕,我的代碼是$ url – Hendry

+1

我不知道你使用的是什麼庫,但是通常的json_decode有特殊的選項來避免使用斜線 - 'echo json_encode($ result,JSON_UNESCAPED_SLASHES);' ' – splash58

回答

0

你必須使用encodeURI(內置的JavaScript函數):

例如:

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

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

這是json()方法的藍圖:

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

使用$options參數設置您想要的逃生選項。要查看完整的JSON常量列表,請檢查以下鏈接:http://php.net/manual/en/json.constants.php

您將需要JSON_UNESCAPED_SLASHES常數來實現您所需的行爲。