[英]How to change form request format to json format
我有一个基于Yii 1.1的应用程序。 后端API出现问题。 就是这种情况。
我是API构建的新手,我受命构建项目的后端API。 现在,从前端提交表单时,我的技术主管说我应该将表单请求格式更改为JSON。 我对此一无所知。 我试图在下面的代码中使用file_gets_content('php://input') and json_decode
,但仍然无法正常工作。
if (isset($_POST['TblTemplate'])) {
//getting raw input of request
var_dump($request = file_get_contents('php://input'));
//decoding the JSON
var_dump($input = json_decode($request, true));
//passing input fields to model attributes
$model->attributes = $input;
//validating input fields (getErrors)
if (!$model->validate()) {
echo json_encode($model->getErrors());
} else {
//inserting (creating) template
if(!$model->save()) {
echo json_encode(['error' => 'Could not create template']);
} else {
echo json_encode(['success' => true]);
exit();
}
}
} else {
$this->render('create',array(
'model'=>$model,
));
}
当我提交表格时,它给我的字段为空。 我对这个API东西很陌生,请帮忙。 另外当我运行var dump或请求它输出时
string(156) "TblTemplate%5Bname%5D=Standard+Feedback+Request&TblTemplate%5Bemail%5D=gideon.a%40scopicsoftware.com&TblTemplate%5Bcontent%5D=I+am+a+new+template&yt0=Create"
但是输入的vardump返回null。
尝试为您起步:
//getting raw input of request
$request = file_get_contents('php://input');
$json = json_decode($request);
if ($json !== null) {
.... code ...
} else {
$this->render('create',array(
'model'=>$model,
));
}
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.