Php laravel 5简单ajax从数据库检索记录

Php laravel 5简单ajax从数据库检索记录,php,laravel,laravel-5,Php,Laravel,Laravel 5,如何使用ajax检索数据?我有我的ajax代码,在我的一些项目中从数据库检索记录时使用过,但不知道如何在Laravel5中使用,因为它有路由和控制器 我有这个html <select name="test" id="branchname"> <option value="" disabled selected>Select first branch</option> <option value="1">branch1</opt

如何使用ajax检索数据?我有我的ajax代码,在我的一些项目中从数据库检索记录时使用过,但不知道如何在Laravel5中使用,因为它有路由和控制器

我有这个html

<select name="test" id="branchname">
    <option value="" disabled selected>Select first branch</option>
    <option value="1">branch1</option>
    <option value="2">branch2</option>
    <option value="3">branch3</option>
</select>

<select id="employees">
    <!-- where the return data will be put it -->
</select>
在我的控制器中,我声明了两个雄辩的模型,模型1用于branchname表,模型2用于employees表

use App\branchname;
use App\employees;
因此,我可以检索如下所示的数据

public function getemployee($branch_no){
    $employees = employees::where("branch_no", '=', $branch_no)->all();
    return $employees;
}
如何返回我从employees表中提取的记录?从ajax第一次与控制器通信并返回对ajax post请求的响应的路由进行布线

任何帮助、建议、建议、想法和线索都将不胜感激。谢谢大家!

注:我是拉威尔5号的新手。

加入你的路线:

Route::post('employees', [
    'as' => 'employees', 'uses' => 'YourController@YourMethod'
]);
阿贾克斯:


首先,在主布局的部分中添加以下条目:

因此,对于需要此_令牌的方法,您无需担心或自行添加csrf_令牌。现在,对于post请求,您可以使用通常的方式使用jQuery向控制器发出Ajax请求,例如:

var data = { id : $(this).val() };
$.post(url, data, function(response){ // Shortcut for $.ajax({type: "post"})
    // ...
});
public function getemployee($branch_no){
    $employees = employees::where("branch_no", $branch_no)->lists('title', 'id');
    return response()->json(['success' => true, 'employees' => $employees]);
}
此处,url应与员工的路线声明的url相匹配,例如,如果您已声明如下路线:

Route::post('employees/{branch_no}', 'EmployeeController@getemployee');
然后,employees是url并返回json响应,以从控制器中填充select元素,因此包含javaScript在内的必需代码如下所示:

$.post('/employees/'+$(this).val(), function(response){
    if(response.success)
    {
        var branchName = $('#branchname').empty();
        $.each(response.employees, function(i, employee){
            $('<option/>', {
                value:employee.id,
                text:employee.title
            }).appendTo(branchName);
        })
    }
}, 'json');

希望您能理解。

首先检查ajax调用发起页面的url example.com/page-usingajax

在AJAX中 如果调用$.get'datalist',sendinput,函数 您实际上是在向GET发出请求 example.com/page-using ajax/datalist

沿途 Route::获取“page-using-ajax/datalist”,xyzController@abc

在控制器方法中

在Ajax成功函数中


我遵循了您的指示,但仍然无法加载资源:服务器的响应状态为500 Internal server Errorerror表示getemployee缺少参数,我确定是$branch\u no参数。如何从ajax post请求中获取发送的id并将其发送到getemployee控制器进行查询?哎呀!我的错误,更新了答案,检查$.postrl+'/'+$this.val,functionresponse{…};现在,选择框中填充了一个选项,该选项在$.eachresponse.employees、functioni、employee{…}内有一个NaNTry employee.id&employee.title文本,我犯了一个错误:pI使用了@Alpha代码指南并集成了您的路线,但它仍然返回我500个内部服务器Errorn.ajaxTransport.k.cors.a.crossDomain.send@jQuery-2.1.3.min.js:4n.extend.ajax@jQuery-2.1.3.min.js:4n.each.n.anonymous function@jQuery-2.1.3.min.js:4anonymous function@main.js:60n.event.dispatch@jQuery-2.1.3.min.js:3n.event.add.r.handle@jQuery-2.1.3.min.js:3 jQuery-2.1.3.min.js:4 POST 500内部服务器错误|错误表示getemployee缺少参数,我确定是$branch_没有参数。如何从ajax post请求中获取发送的id并将其发送到getemployee控制器进行查询?
var data = { id : $(this).val() };
$.post(url, data, function(response){ // Shortcut for $.ajax({type: "post"})
    // ...
});
Route::post('employees/{branch_no}', 'EmployeeController@getemployee');
$.post('/employees/'+$(this).val(), function(response){
    if(response.success)
    {
        var branchName = $('#branchname').empty();
        $.each(response.employees, function(i, employee){
            $('<option/>', {
                value:employee.id,
                text:employee.title
            }).appendTo(branchName);
        })
    }
}, 'json');
public function getemployee($branch_no){
    $employees = employees::where("branch_no", $branch_no)->lists('title', 'id');
    return response()->json(['success' => true, 'employees' => $employees]);
}
if (Request::ajax())
    {
        $text = \Request::input('textkey');
        $users = DB::table('datalists')->where('city', 'like', $text.'%')->take(10)->get();
        $users = json_encode($users);
        return $users;
    }
function(data) {
    data = JSON.parse(data);
    var html = "";
    for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].city + "'>";
    };
    $("#datalist-result").html(html);
}