开发的前端正在通过 Ajax 调用发送格式化为 JSON 对象的数据数组。 json 对象如下所示:
{
"name": " Test Name ",
"image_url": "test URL",
"include": [
"1"
],
"dimension": [
null
],
"media_type": [
null
],
"match": [
"1"
],
"content": [
"test content"
],
"sorting": {
"rating": "50",
"language": "50",
"CS Weight": "50",
}
}
如何在我的 PHP Controller 中读取它。我可以这样得到它吗:
$data = $_POST;
因为在本例中包含 JSON 对象的变量没有名称,所以我无法通过这种方式获取它
$data = $_POST['data']
编辑部分
<小时/>从前端,数据是这样发送的:
sendAjax: function(value, url, callback){
xhr = $.ajax({
type: 'POST',
url: url,
data: value
}).done(function(message){
callback(message);
}).fail(function(jqXHR, textStatus){
console.log('failed to submit form, error type: '+textStatus);
});
}
最佳答案
从脚本的输入中读取它,您可以在其中获取“原始”POST 数据:
$json = file_get_contents('php://input');
$data = json_decode($json);
关于javascript - 读取通过 Ajax 调用发送的 PHP 中的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617900/