javascript - 从 laravel 中的 Controller 返回 JSON 响应

标签 javascript json ajax laravel

我是 Javascript 新手。我想在我的表单中进行 ajax 更新。一切正常。更新完成后,该值将在数据库中更新。问题发生的地方是当我尝试在 View 中更新它时。这是我的 Controller

public function updatePost(Request $request)
{

    $post = $request->all();
    $val = \Validator::make($request->all(), [

            'client_id' => 'required',
            'client_name' => 'required',
            'client_business' => 'required',
            'client_ref' => 'required',
            'gmail_mail' => 'required',
            'gmail_pass' => 'required',
            'client_dob' => 'required',
            'client_addr' => 'required',
            'client_no1' => 'required',
            'client_ref' => 'required',
            'domain_name' => 'required',
            'domain_p_date' => 'required',
            'domain_reg' => 'required',
            'domain_ex_date' => 'required',
            'domain_acc_email' => 'required',
            'domain_acc_pass' =>  'required',
            'domain_id' => 'required',

        ]);

            if ($val->fails()) {

            return redirect()->back()->withErrors($val->errors());
        }


             else {

                $data = array(

                'client_id' => $post['client_id'],
                'client_name' => $post['client_name'],
                'client_business' => $post['client_business'],
                'client_ref' => $post['client_ref'],
                'gmail_mail' =>  $post['gmail_mail'],
                'gmail_pass'=> $post['gmail_pass'],
                'client_dob'=> $post['client_dob'],
                'client_addr'=> $post['client_addr'],
                'client_no1'=> $post['client_no1'],

                'domain_name' => $post['domain_name'],
                'domain_p_date' => $post['domain_p_date'],

                'domain_reg' => $post['domain_reg'],
                'domain_ex_date' => $post['domain_ex_date'],
                'domain_acc_email' => $post['domain_acc_email'],
                'domain_acc_pass' => $post['domain_acc_pass'],
                'domain_id' => $post['domain_id'],

                );

            //var_dump($data);

           $update_data = domain_details::where('domain_id', $post['domain_id'])
           ->update($data);

                if ($update_data) {

                    $new_data = domain_details::where('domain_id',$post['domain_id'])->get();

                    return response()->json( ['client_id'=> 'johncena'],200);
                }
                else
                {

                    return redirect()->back()->withErrors($val->errors());
                }



        }

这是我从表单收到并更新的数据。数据库更新工作正常。问题就出在这里

if ($update_data) {

$new_data = domain_details::where('domain_id',$post['domain_id'])->get();

return response()->json( ['client_id'=> $new_data->client_id],200);

json( ['client_id'=> $new_data->client_id],200); 如果我只传递一个字符串,它可以像 json( ['client_id'=> '李四'],200);.

另一个问题是当我看到控制台时。如果我尝试在不更改任何内容的情况下更新表单,则会引发 500 内部服务器错误。这是我处理来自 Controller 的响应的 Javascript 代码。有时字符串会更改为 John Doe,大多数时候我会收到 500 内部服务器错误。

$('#update-modal').on('click',function(){

$.ajax({

    type : "get",
    url : updateURL,
    data : { client_id : $('#client_id').val(),
             client_name : $('#client_name').val(),
             client_business : $('#client_business').val(),
             client_ref : $('#client_ref').val(),
             gmail_mail : $('#gmail_mail').val(),
             gmail_pass : $('#gmail_pass').val(),
             client_dob : $('#client_dob').val(),
             client_addr : $('#client_addr').val(),
             client_no1 : $('#client_no1').val(),

             domain_name : $('#domain_name').val(),
             domain_p_date : $('#domain_p_date').val(),
             domain_reg : $('#domain_reg').val(),
             domain_ex_date : $('#domain_ex_date').val(),
             domain_acc_email : $('#domain_acc_email').val(),
             domain_acc_pass :  $('#domain_acc_pass').val(),
             domain_id : domain_id,
             _token : token 
         }

})
.done(function(msg){
    $(client_id_element).text(msg['client_id']);



});
});

我知道我错过了一些东西。我不知道它是什么。提前致谢

最佳答案

嗯,我认为 Eloquent get() 返回的是一个集合,而不是某个对象。看起来您只想返回一个具有唯一 domain_id 的对象。尝试使用

$new_data = domain_details::where('domain_id',$post['domain_id'])->first();

相反。

关于javascript - 从 laravel 中的 Controller 返回 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39591680/

相关文章:

javascript - JS RegExp 捕获每场比赛的所有组和位置

java - WebResource put 去掉requestEntity的 '+'字符

ios - JSON序列化错误

json - 如何在Reduce函数中显示值

php - 客户端模板

javascript - ajax 中的警报甲酸盐没有得到响应

javascript - 如何判断 Javascript 加载的脚本 src 是 http 状态代码 200 还是 301?

javascript - 如何使用 REST API 将表单数据从 ionic 应用程序发送到 parse.com MBaaS

javascript - 无法从浏览器调试 Node js 应用程序

javascript - 输入 JavaScript 后修改字符或单词