dongli7236 2016-03-08 13:56
浏览 54
已采纳

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

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条回答 默认 最新

  • douduan7295 2016-03-08 14:27
    关注

    //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;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?