【问题标题】:Laravel get data from controller and send to view ajaxLaravel 从控制器获取数据并发送到查看 ajax
【发布时间】:2019-05-10 09:57:43
【问题描述】:

我正在尝试从控制器获取数据并在 ajax 中发送。 该脚本位于 app.blade.php 中。

控制器

public function show()
{
    $number=Order::where('user_id',Auth::user()->id)->count();

    return $number;

}

路线

Route::get('product_number', 'OrderController@show')->name('product_number');

JS

<script type="text/javascript">
$(document).ready(function(){

    $.ajax({
        type: "GET",
        url: "product_number",
        success: function (data) {
            console.log(data);
        },
        error: function () {
            console.log('Error');
        }
    });

});
</script>

在控制台中,它显示了 html 视图。

【问题讨论】:

  • 你的js中没有alert。您的意思是 console.log 没有显示任何内容还是?
  • 我编辑了它。它向我展示了 HTML 视图
  • @alphal,你能在这里显示控制台的内容吗?
  • @alphal 表示有错误
  • @AkashKumarVerma 我该怎么办?

标签: ajax laravel


【解决方案1】:

试试这个,你可以得到错误

public function show()
{
    try {
        $number=Order::where('user_id',Auth::user()->id)->count();
    } catch (\Exception $e) {
        echo $e->getMessage(); die;
    }
    return $number;
}
$(document).ready(function(){

    $.ajax({
        type: "GET",
        url: "{{url('product_number')}}",
        success: function (data) {
            console.log(data);
        },
        error: function () {
            console.log('Error');
        }
    });

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2020-09-24
    • 1970-01-01
    • 2018-03-16
    • 2019-10-21
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    相关资源
    最近更新 更多