首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ajax中发送json格式的变量

在ajax中发送json格式的变量
EN

Stack Overflow用户
提问于 2018-03-09 00:13:50
回答 1查看 42关注 0票数 0

我试图发送2个变量从php使用json,其中携带HTML表TR与TD在AJAX。

代码语言:javascript
复制
    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填充到表中?

EN

回答 1

Stack Overflow用户

发布于 2018-03-09 05:05:15

因为默认情况下响应格式是HTML,所以您应该只在操作方法中返回一个字符串。如果您希望使用不同的响应格式,则应在返回数据之前先进行设置。例如,

代码语言:javascript
复制
public function actionInfo()
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    return [
        'message' => 'hello world',
        'code' => 100,
    ];
}

现在看看你的代码,你不是在响应response,也没有在响应中返回exception消息,你应该在响应数组中添加一个msg索引,然后在js端检查它,如果是空的,使用deductionallowance html,如果不是空的,那么警告msg,以便显示错误/异常。

代码语言:javascript
复制
 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
        ];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49177656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档