【发布时间】:2021-06-17 11:17:26
【问题描述】:
我正在尝试从 PHP 获取 JSON 值,但如果其中一个属性值包含正斜杠“/”,则会失败(我正在使用 GET 来发送此特定场景的数据)。
这是我发送数据的方式(当我不发送“/”时,这很好用)。
用户界面方面
const dataObj = {
description: 'my / description',
buyer: 'Mike Brown'
};
const dataString = JSON.stringify(dataObj);
fetch(`http://localhost:8000/clients/${dataString}`)
.then((response) => response.text())
.then((responseData) => {
......
});
PHP端:
Route::get('/clients/{data}', function($data) {
// This line below fails ONLY if value of description property contains "/"
// otherwise it works just fine
$dataToBeSaved = json_decode($data, true);
});
是的,我对 json_decode 做了一些研究,但没有很清楚。谁能指出我正确的方向?提前非常感谢!
【问题讨论】:
-
试试这样,对我有用:
$personJSON = '{"name":"Johny / Carson","title":"CTO"}'; $person = json_decode($personJSON); echo "Name: ". $person->name . '<br/>'; echo "Title: ". $person->title; -
您需要将其作为获取请求发送吗?如果您可以将其作为发布请求发送,这将更容易
-
您解决了这个问题吗?
标签: javascript php laravel laravel-5