Php 尽管查询字符串参数正确,但Ajax GET请求为空

Php 尽管查询字符串参数正确,但Ajax GET请求为空,php,ajax,laravel,Php,Ajax,Laravel,使用一个简单的ajaxget请求来检索一些数据,它成功地检查了if($request->Ajax()){},但是由于request$request变量中没有数据,因此任何验证都失败。这只在生产服务器上发生,在本地主机上一切正常 控制台显示预期的URLhttps://example.com/employeeInfo?id=1,然后出现错误422(无法处理的实体)。error:function(jqxhr,status,exception){alert('exception:',exception)

使用一个简单的ajaxget请求来检索一些数据,它成功地检查了
if($request->Ajax()){}
,但是由于
request$request
变量中没有数据,因此任何验证都失败。这只在生产服务器上发生,在本地主机上一切正常

控制台显示预期的URL
https://example.com/employeeInfo?id=1
,然后出现错误422(无法处理的实体)。
error:function(jqxhr,status,exception){alert('exception:',exception);}的输出给出一条空的警报消息

查看

<script>
(function ($) {
$(document).ready(function() {
    $(".team-pic").off("click").on("click", function() {
         $id = $(this).data('id');

         // Get data
         $.ajax({
            type: 'GET',
            url: 'employeeInfo',
            data: {'id':$id},
            success: function(data){
                var obj=$.parseJSON(data);
                // Show output...
            },

             error: function(jqxhr, status, exception) {
                alert('Exception:', exception);
            }
        });

      });
});
}(jQuery));
</script>
控制器

public function get(Request $request) {

    if($request->ajax()) {

        $this->validate($request, [
            'id' => 'required|integer',
        ]);

        // Id
        $employee = Employee::find(request('id'));

        // Create output
        $data = ...
        echo json_encode($data);

    }
}
public function get(Employee $employee)
{
    // The id being valid is already done by forcing it to be an Employee
    // It is also an ajax call because it is going to the api route
    // This will json_encode the employee object.
    return $employee;
}

如果我是你,我会使用RESTful API,特别是显式绑定

RouteServiceProvider.php

public function boot() 
{
    parent::boot();
    Route::model('employee', App\Employee::class);
}
路线

Route::get('/employeeInfo', 'EmployeeController@get');
Route::get('api/employees/{employee}', 'EmployeeController@get');
控制器

public function get(Request $request) {

    if($request->ajax()) {

        $this->validate($request, [
            'id' => 'required|integer',
        ]);

        // Id
        $employee = Employee::find(request('id'));

        // Create output
        $data = ...
        echo json_encode($data);

    }
}
public function get(Employee $employee)
{
    // The id being valid is already done by forcing it to be an Employee
    // It is also an ajax call because it is going to the api route
    // This will json_encode the employee object.
    return $employee;
}

如果我是你,我会使用RESTful API,特别是显式绑定

RouteServiceProvider.php

public function boot() 
{
    parent::boot();
    Route::model('employee', App\Employee::class);
}
路线

Route::get('/employeeInfo', 'EmployeeController@get');
Route::get('api/employees/{employee}', 'EmployeeController@get');
控制器

public function get(Request $request) {

    if($request->ajax()) {

        $this->validate($request, [
            'id' => 'required|integer',
        ]);

        // Id
        $employee = Employee::find(request('id'));

        // Create output
        $data = ...
        echo json_encode($data);

    }
}
public function get(Employee $employee)
{
    // The id being valid is already done by forcing it to be an Employee
    // It is also an ajax call because it is going to the api route
    // This will json_encode the employee object.
    return $employee;
}

仅供参考,如果要使用
警报
,它只需要一个字符串参数。例如
警报(状态+:'+异常)
。就个人而言,我会使用
控制台。错误(状态,异常)
我认为您在
$employee=employee…
行中要查找的是:
$employee=employee::find($request->input('id')另外,我认为在您的
验证
行中,我认为您需要
$request->all()
什么
request()
函数<代码>如果($request->ajax())…
?我可以省略它,但是
$request
变量仍然是空的。感谢您的
控制台。错误
建议。然而,它并没有提供新的信息。@Phil发现
请求
助手在使用它时起作用:这也使得我上面评论的前半部分不正确。@ajon干杯!我对Laravel不太了解,也没想到在如此严格的OOP框架中会有全局函数可用。您了解的越多,呃,仅供参考,如果您要使用
警报
,它只需要一个字符串参数。例如
警报(状态+:'+异常)
。就个人而言,我会使用
控制台。错误(状态,异常)
我认为您在
$employee=employee…
行中要查找的是:
$employee=employee::find($request->input('id')另外,我认为在您的
验证
行中,我认为您需要
$request->all()
什么
request()
函数<代码>如果($request->ajax())…
?我可以省略它,但是
$request
变量仍然是空的。感谢您的
控制台。错误
建议。然而,它并没有提供新的信息。@Phil发现
请求
助手在使用它时起作用:这也使得我上面评论的前半部分不正确。@ajon干杯!我对Laravel不太了解,也没想到在如此严格的OOP框架中会有全局函数可用。你学的越多,嗯,太棒了!我以前没有使用路由模型绑定,但仔细阅读后,我得到了您的解决方案,并对其进行了一些修改,使其更加简单:左
RouteServiceProvider
原样,route
route::get('employee/{employee},'EmployeeController@get');,以及您建议的控制器。我相信你的回答是正确的。谢谢杰出的我以前没有使用路由模型绑定,但仔细阅读后,我得到了您的解决方案,并对其进行了一些修改,使其更加简单:左
RouteServiceProvider
原样,route
route::get('employee/{employee},'EmployeeController@get');,以及您建议的控制器。我相信你的回答是正确的。谢谢