Laravel:使用Ajax时表单提交页面已过期

时间:2018-06-26 17:37:55

标签: ajax laravel-5.6

我是Laravel的新手,并且在过去三天都陷入了提到的问题。我用谷歌搜索,但没有找到任何解决方案。

我已经在Laravel中创建了一个表单,并且由于失去了Grower代码输入的关注,我正在使用Ajax Get方法从数据库中获取客户。Fetched data from database Ajax代码如下:

CIImage

问题

当我按下“提交”按钮将其保存到另一个表时,它显示一个错误页面,指出该页面由于不活动而过期

到目前为止我尝试过的事情

似乎此错误是由于CSRF保护所致,因此

  • 我使用了隐藏输入来保存csrf值。

    $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#txtGrowerCode").focusout(function(){ var grower_code = $("#txtGrowerCode").val(); var grower_name = $("#txtGrowerName"); var father_name = $("#txtFatherName"); var village_name = $("#txtVillageName"); var society_name = $("#txtSocietyName"); var distt_name = $("#txtDisttName"); $.ajax({ type : "get", url : "api/grower_details/"+grower_code, datatype : "json", data : $("#hidden").val(), success: function(options){ obj = jQuery.parseJSON(options) $.each(obj, function(i,option){ grower_name.val(option.g_name); father_name.val(option.g_father); village_name.val(option.v_name); society_name.val(option.so_name); distt_name.val(option.d_name); }); } }); });

  • 添加了元标记

    <input type="hidden" id="_hidden" name="_hidden" value="{{csrf_token()}}">

  • 通过此方法,我尝试从VerifyCsrfToken.php中排除当前URL 我能够成功提交表格,但会使 登录会话或破坏我的会话变量。

  • 我删除了我的Ajax代码,即未从Ajax代码中获取客户数据 数据库(我手动填写了所有字段),然后尝试提交 通过此数据成功保存表格。

代码:

路由文件

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

API文件

// Route group soil sample collection
Route::group(['Middleware'=>['web']],function(){
        Route::GET('/soil_sample',[
        'as' => 'soil_sample',
        'uses' => 'soilSampleDetailsController@getSampleDetails',
    ]);
        Route::post('/soil_sample',[
        'uses' => 'soilSampleDetailsController@createSampleDetails',
    ]);
});

控制器代码

Route::group(['middleware' => ['web']],function(){
    Route::get('grower_details/{grower}',[
        'as' => 'grower_details',
        'uses' => 'soilSampleDetailsController@getGrowerDetails'
    ]);
});

模型文件:

public function createSampleDetails(){
        $this->validate(request(),[
            'txtGrowerCode' => 'required',
            'txtGrowerName' => 'required',
            'txtFatherName' => 'required',
            'txtVillageName' => 'required',
            'txtSocietyName' => 'required',

        ],
            ['txtGrowerCode.required' => 'Grower Code is required!',
            'txtGrowerName.required' => 'Grower Name is required!',
            'txtFatherName.required' => 'Father name is required!',
            'txtVillageName.required' => 'Village Name is required!',
            'txtSocietyName.required' => 'Society Name is required']
        );


        $sampple_detail = new sample_detail;
        $sampple_detail->addSampleDetails();

        return redirect('/soil_sample');
    }
public function getGrowerDetails(Request $request){     
        if($request->ajax()){
            $data = new Grower();
            $growerDetails = $data->getGrowerDetails($request->grower);
            return json_encode($data->getGrowerDetails($request->grower));  
        }   
    }

表单标签

public function addSampleDetails()
    {
        $this->s_grower = request('txtGrowerCode');
        $this->crop_name = request('txtCrop');
        $this->save();
    }


public function getGrowerDetails($grower_code){
        $grower_details = self::leftJoin('society_master', 'g_soc','so_code')
            ->leftJoin('village_master','g_vill','v_code')
            ->leftjoin('district_master','district_master.d_code','society_master.so_distt_code')
            ->select('grower_master.id', 'grower_master.g_code', 'grower_master.g_name', 'grower_master.g_father','society_master.so_name', 'village_master.v_name', 'district_master.d_name')
                ->where('grower_master.g_code',$grower_code)
            ->get();
        return $grower_details;
    }

0 个答案:

没有答案