简体   繁体   English

从Laravel中的控制器返回JSON响应

[英]Returning JSON response from controller in laravel

I am new to Javascript. 我是Java语言的新手。 I want to do a ajax updation in my form. 我想在表单中进行Ajax更新。 Everything works fine. 一切正常。 once the update is done the value is updated in the database. 一旦完成更新,值就会在数据库中更新。 where the problem occurs is when i try to update it in the view. 发生问题的地方是当我尝试在视图中更新它时。 Here is my 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());
                }



        }

this is the data i receive from the form and update. 这是我从表单接收并更新的数据。 Updation on the Databse works fine. 在数据库上更新工作正常。 This is where the problem occurs 这是发生问题的地方

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'=> $ new_data-> client_id],200); if i pass just a string it works fine like j son( ['client_id'=> 'John Doe'],200); 如果我只传递一个字符串,它将像j son(['client_id'=>'John Doe'],200)一样正常工作; .

The other problem is when i see the console. 另一个问题是当我看到控制台时。 If i try to update the form without changing anything it throws a 500 internal server error. 如果我尝试不更改任何内容就更新表格,则会引发500个内部服务器错误。 Here is my Javascript code that handles the response from the controller. 这是我的Javascript代码,用于处理来自控制器的响应。 Sometime the string is changed to John Doe and most of the times i get 500 internal Server error. 有时,字符串更改为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']);



});
});

I know i am missing something. 我知道我想念一些东西。 I cant figure out what it is. 我不知道是什么。 Thanks in advance 提前致谢

Well, I think that Eloquent get() returns a collection, not a certain object. 好吧,我认为Eloquent get()返回一个集合,而不是某个对象。 Looks like you want to return just one object with domain_id that is unique. 看起来您只想返回一个具有domain_id唯一的对象。 Try using 尝试使用

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

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM