【问题标题】:Fetching the data from Database laravel using Ajax call使用 Ajax 调用从数据库 laravel 中获取数据
【发布时间】:2018-08-05 16:27:02
【问题描述】:

尝试从本地数据库中“获取”值并尝试在控制台上打印。

控制器方法

public function getUpdate(Request $request)
{
     if($request->ajax()){
     $customer = Customers::find($request->id);
     return Response($customer);
    }
}

Customer.blade.php Ajax 脚本。

 $('tbody').delegate('.btn-edit','click',function(){
     console.log('Monitoring');
     var value = $(this).data('id');
     console.log(value);
     var url='{{URL::to('getUpdate')}}';
     console.log(url);
     $.ajax({
        type : 'get',
        url  : url,
        data : 'success',
        sucess:function(data){
          console.log(data);
        },
        error:function(data){
          console.log('Failed');
          console.log(data);
        }
     });
   });

试图通过 ID 获取数据。

数据库字段。 Id , first_name, last_name 等等。

路由页面代码

Route::get('/getUpdate','CustomerController@getUpdate');

在控制台上什么也得不到。 尝试从邮递员那里获取它

localhost/public/getUpdate?id:1

但无法得到任何东西。

【问题讨论】:

  • 你写的是“成功:函数...”而不是“成功:函数...”

标签: ajax laravel


【解决方案1】:

你犯了一些错误

  • 你没有发送 id
  • 成功函数中的拼写错误

这是你的解决方案

$.ajax({
    type : 'get',
    url  : url,
    data : {'id':your data},
    success: function(res){
      console.log(res);
    },
    error: function(res){
      console.log('Failed');
      console.log(res);
    }
 });

【讨论】:

  • 非常感谢。你解决了我的问题。我现在可以得到它。
【解决方案2】:

您应该将值作为数组返回或使用它 在 laravel 5.6 中尝试

return response()->json([
    'name' => 'Abigail',
    'state' => 'CA'
]);

或在您的代码上

public function getUpdate(Request $request)
{
     if($request->ajax()){
     $customer = Customers::find($request->id);
     return Response()->json($customer);
    }
}

查看此文档了解更多信息

https://laravel.com/docs/5.6/responses

【讨论】:

  • 感谢您的宝贵回复和所附文件:)
猜你喜欢
  • 2018-02-11
  • 2019-11-05
  • 2012-12-14
  • 1970-01-01
  • 2019-11-26
  • 2023-03-27
  • 1970-01-01
  • 2020-08-22
  • 2011-08-26
相关资源
最近更新 更多