0

I want to retrieve the data values in a controller function which I am sending through ajax, but unable to achieve it.

Here is my ajax code -

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);          
    });

and here is my laravel controller function -

<?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();

  }
}

Here is my route -

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

When I check with postman, then I get this response-

array(0) { }

I have seen many stackoverflow links but none of them worked out for me. Please someone help me to retrieve the data.

1
  • you are not sending your CSRF token which laravel requires for all post data Commented Mar 8, 2016 at 14:05

1 Answer 1

1

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

// for routing

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

//for controller

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

}
}

//now you will be able to see ur input data in console.

//dont forget to include in controller

use Illuminate\Support\Facades\Input;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.