在ajax中的json中发送变量

时间:2018-03-08 16:13:50

标签: php ajax yii2

我正在尝试使用json从php发送2个变量,json在AJAX中携带带有TD的HTML TABLE TR。

    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?

1 个答案:

答案 0 :(得分:0)

因为默认情况下响应格式是HTML,所以只应在action方法中返回一个字符串。如果要使用其他响应格式,则应在返回数据之前先进行设置。例如,

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

现在查看您的代码encoding response并且您没有在响应中返回exception消息,您应该向msg索引添加js索引。响应数组然后在deduction侧检查它是否为空它们使用allowancemsg html如果不为空则提醒 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 ]; } } 以便错误/异常是显示。

public class BlockTester
{
    private static TransformBlock<int, int> _worker;

    public static async Task StartAsync()
    {
        _worker = new TransformBlock<int, int>(s => s + s);
        var buffer = new BufferBlock<int>();
        var consumeTask = Consume(buffer);

        _worker.LinkTo(buffer, new DataflowLinkOptions{PropagateCompletion = true});

        foreach (var value in Enumerable.Range(0,100))
        {
            _worker.Post(value);
        }

        _worker.Complete();

        await buffer.Completion;

        if(buffer.TryReceiveAll(out var received))
        {
            Console.WriteLine(string.Join(", ", received));
        }
    }

    public static async Task<IReadOnlyCollection<int>> Consume(ISourceBlock<int> buffer)
    {
        var received = new List<int>();

        while (await buffer.OutputAvailableAsync())
        {
            var current = buffer.Receive();

            received.Add(current);

            if (current > 25)
            {
                _worker.Complete();
            }
        }

        return received;
    }
}