douzai3399 2015-08-09 23:55
浏览 54
已采纳

laravel - 从http请求中获取参数

I want to pass in multiple parameters from my Angular app to my Laravel API, namely the id and choices array supplied by user.

Angular:

http request:

    verifyAnswer: function(params) {
            return $http({
                method: 'GET',
                url: 'http://localhost:8888/api/questions/check',
                cache: true,
                params: {
                    id: params.question_id,
                    choices: params.answer_choices
                }
            });

Laravel 5:

routes.php:

$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');

ApiController.php:

public function getAnswer(Request $request) {
    die(print_r($request));
}

I thought I should use :any within my URI to indicate I'll be passing in an arbitrary amount of parameters of various data structure (id is a number, choices is an array of choices).

How can I make this request?


[200]: /api/questions/check?choices= choice+1 &choices= choice+2 &choices= choice+3 &id=1

  • 写回答

1条回答 默认 最新

  • donglisi8644 2015-08-09 23:58
    关注

    Change this:

    $router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');
    

    to

    $router->get('/api/questions/check', 'ApiController@getAnswer');
    

    And fetch the values with

    echo $request->id;
    echo $request->choices;
    

    in your controller. There is no need to specify that you will receive parameters, they will all be in $request when you inject Request to your method.

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动