无法在控制器中检索ajax数据 - Laravel 5.2,AngularJs

时间:2016-03-08 13:56:28

标签: php angularjs ajax controller laravel-5.2

我想在控制器函数中检索数据值,我通过ajax发送但无法实现它。

这是我的ajax代码 -

var postArg = {startDate:localStorage.getItem('startDate'), endDate:localStorage.getItem('endDate'), startTime:localStorage.getItem('startTime'), endTime:localStorage.getItem('endTime')}

    $http.post('checkvehicleavailability', angular.toJson(postArg), {    
        withCredentials: true,      
        headers: {'Content-Type': "application/x-www-form-urlencoded" }           
    }).success(function(data, status) {                                    
        console.log(data);          
    });

这是我的laravel控制器功能 -

<?php

 namespace App\Http\Controllers;

 use App\User;
 use Illuminate\Routing\Controller;
 use App\Http\Controllers\Input;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Redirect;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Support\Facades\Validator;




class VehicleController extends Controller
{
    public function vehicleAvailability(){


    $request = new Request();
    $data = $request->all();
    var_dump($data);
    die();

  }
}

这是我的路线 -

Route::post('checkvehicleavailability','VehicleController@vehicleAvailability');

当我与邮递员核实时,我得到了这个回复 -

array(0) { }

我见过很多stackoverflow链接,但没有一个能为我解决。请有人帮我检索数据。

1 个答案:

答案 0 :(得分:1)

// ajax电话     $.ajax({ type: "POST", url: "{{URL::to('checkvehicleavailability')}}", data: {datahere}, dataType: 'json', success: function(response){ console.log(response); } });

//用于路由

Route::post('checkvehicleavailability','VehicleController@vehicleAvailability');

// for controller

class VehicleController extends Controller
{
public function vehicleAvailability(){
$inputs = Input::all();
return Response::json( $inputs);

}
}

//现在您将能够在控制台中看到您的输入数据。

//别忘了包含在控制器中

use Illuminate\Support\Facades\Input;