简体   繁体   English

Laravel和AJAX不返回任何内容

[英]Laravel and AJAX doesn't return anything

I want to return data from my database with .ajax() but it throws out an error with the entire HTML of a page. 我想使用.ajax()从数据库中返回数据,但它会引发页面的整个HTML错误。 Why is it doing that? 为什么这样做呢?

My .ajax() call: 我的.ajax()呼叫:

$.ajax({
    url: '{{ URL('reports/groupsUsersGet') }}',
    dataType: "json",
    data: {
        group_id : $('#group').val(),
    },
    success: function(data) {
        alert(data);
    },
    error: function (data) {
        console.log('Error:', data);
    }
});

route 路线

Route::get('reports/groupsUsersGet',
    array(
        'as' =>'groupsUsersGet',
        'uses' => 'ReportsController@groupsUsersGet'
    )
);

view(form) 视图(窗体)

{{ Form::select('grup',$group,null,['class'=>'form-control','id'=>'group']) }}

controller 控制者

$term = Input::get('group_id');
$results = array();
DB::table('users')->where('group', 'LIKE', '%'.$term.'%')->get();

foreach ($queries as $query) {
    $results[] = [
        'id' => $query->id,
        'value' => $query->nick
    ];
}

return Response::json($results);

Also send the csrf_token() as data. 还发送csrf_token()作为数据。

 $.ajax({
        url: '{{ URL('reports/groupsUsersGet') }}',
        dataType: "json",
        data: {
         _token: <?php echo csrf_token();?>,
          group_id : $('#group').val(),
        },
        success: function(data) {
          alert(data);
        },
        error: function (data) {
          console.log('Error:', data);
        }
      });

It seems to me that you aren't sending CSRF token in the request, as @HikmatSijapati specified. 在我看来,您没有按照@HikmatSijapati的指定在请求中发送CSRF令牌。 In ajax request you can pass csrf token like this: 在ajax请求中,您可以像这样传递csrf令牌:

You could, for example, store the token in a HTML meta tag: 例如,您可以将令牌存储在HTML元标记中:

<meta name="csrf-token" content="{{ csrf_token() }}">

Then, once you have created the meta tag, you can instruct a library like jQuery to automatically add the token to all request headers. 然后,一旦创建了meta标签,就可以指示jQuery之类的库将令牌自动添加到所有请求标头中。 This provides simple, convenient CSRF protection for your AJAX based applications: 这为基于AJAX的应用程序提供了简单便捷的CSRF保护:

$.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

Hope this helps! 希望这可以帮助!

Try this: 尝试这个:

In your controller you can't define $queries variable inside your method. 在您的控制器中,您无法在方法内部定义$queries变量。

ajax call 阿贾克斯电话

$.ajax({
    url: "{{ URL('reports/groupsUsersGet') }}",
    method: 'GET',
    dataType: "json",        
    data: {
        group_id : $('#group').val(),
    },
    success: function(data) {
        alert(data);
    },
    error: function (data) {
        console.log('Error:', data);
    }
});

controller 控制者

$term = Input::get('group_id');
$results = array();
$queries = DB::table('users')->where('group', 'LIKE', '%'.$term.'%')->get();

foreach ($queries as $query) {
    $results[] = [
        'id' => $query->id,
        'value' => $query->nick
    ];
}

return Response::json(['results' => $results], 200);

Thank you everyone for your help but the error was in my not including use Response at the top of controller. 谢谢大家的帮助,但错误在于我未在控制器顶部使用“响应”。 When I did that it worked. 当我这样做时,它起作用了。

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

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