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;

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

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题