我试图发送2个变量从php使用json,其中携带HTML表TR与TD在AJAX。
public function actionAllowancelist(){
$Role = UserController::CheckRole("payroll");
if($Role == true){
try {
$query = new Query();
$allowances = $query->select(['PayrollSettingID', 'IsAllowance','Title','Amount','Formula'])->from('payrollsetting')->where(['IsActive'=>1])->all();
$allow=NULL;
$dedu=NULL;
if($allowances != NULL && sizeof($allowances) > 0){
foreach ($allowances as $allowance){
if($allowance['IsAllowance'] == 0){
$allow .='<tr data-id="">';
$allow.='<td>'.$allowance['Title'].'</td>';
if ($allowance['Formula'] != NULL) {
$allow.='<td>'.$allowance['Amount'].'</td>';
$allow.='<td hidden="true">'.$allowance['Formula'].'</td>';
}else{
$allow.='<td contenteditable = "true">'.$allowance['Amount'].'</td>';
$allow.='<td hidden="true">'.$allowance['Formula'].'</td>';
}
$allow .='</tr>';
}else{
$dedu .='<tr data-id="">';
$dedu.='<td>'.$allowance['Title'].'</td>';
if ($allowance['Formula'] != NULL) {
$dedu.='<td>'.$allowance['Amount'].'</td>';
$dedu.='<td hidden="true">'.$allowance['Formula'].'</td>';
}else{
$dedu.='<td contenteditable = "true">'.$allowance['Amount'].'</td>';
$dedu.='<td hidden="true">'.$allowance['Formula'].'</td>';
}
$dedu .='</tr>';
}
}
}
} catch (Exception $e) {
return $e;
}
return '{"allowance":'.$allow',"deduction":'.$dedu.'}';
}
}//function ends here在前端的Ajax中,我只是尝试获取这些值,而dataType是JSON。如何让前端的这些表TD填充到表中?
发布于 2018-03-09 05:05:15
因为默认情况下响应格式是HTML,所以您应该只在操作方法中返回一个字符串。如果您希望使用不同的响应格式,则应在返回数据之前先进行设置。例如,
public function actionInfo()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return [
'message' => 'hello world',
'code' => 100,
];
}现在看看你的代码,你不是在响应response,也没有在响应中返回exception消息,你应该在响应数组中添加一个msg索引,然后在js端检查它,如果是空的,使用deduction和allowance html,如果不是空的,那么警告msg,以便显示错误/异常。
public function actionAllowancelist() {
$Role = UserController::CheckRole ( "payroll" );
$msg = '';
if ( $Role == true ) {
try {
$query = new Query();
$allowances = $query->select ( [ 'PayrollSettingID' , 'IsAllowance' , 'Title' , 'Amount' , 'Formula' ] )->from ( 'payrollsetting' )->where ( [ 'IsActive' => 1 ] )->all ();
$allow = NULL;
$dedu = NULL;
if ( $allowances != NULL && sizeof ( $allowances ) > 0 ) {
foreach ( $allowances as $allowance ) {
if ( $allowance['IsAllowance'] == 0 ) {
$allow .= '<tr data-id="">';
$allow .= '<td>' . $allowance['Title'] . '</td>';
if ( $allowance['Formula'] != NULL ) {
$allow .= '<td>' . $allowance['Amount'] . '</td>';
$allow .= '<td hidden="true">' . $allowance['Formula'] . '</td>';
} else {
$allow .= '<td contenteditable = "true">' . $allowance['Amount'] . '</td>';
$allow .= '<td hidden="true">' . $allowance['Formula'] . '</td>';
}
$allow .= '</tr>';
} else {
$dedu .= '<tr data-id="">';
$dedu .= '<td>' . $allowance['Title'] . '</td>';
if ( $allowance['Formula'] != NULL ) {
$dedu .= '<td>' . $allowance['Amount'] . '</td>';
$dedu .= '<td hidden="true">' . $allowance['Formula'] . '</td>';
} else {
$dedu .= '<td contenteditable = "true">' . $allowance['Amount'] . '</td>';
$dedu .= '<td hidden="true">' . $allowance['Formula'] . '</td>';
}
$dedu .= '</tr>';
}
}
}
} catch ( Exception $e ) {
$msg = $e->getMessage ();
}
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return [
'msg' => $msg ,
'allowance' => $allow ,
'deduction' => $dedu
];
}
}https://stackoverflow.com/questions/49177656
复制相似问题