【问题标题】:Send two parameters with ajax get method when using laravel's route contoller使用laravel路由控制器时用ajax get方法发送两个参数
【发布时间】:2015-06-21 07:52:05
【问题描述】:

假设我们有一个这样的控制器和路由:

Route::controller('/test', TestController);

我们还有控制器方法,

public function getIndex($data)
{
  return $data;
}

这对ajax没问题,get方法

$.ajax(
 {
  url: '/test/' + data //this data comes from some hidden input
  success: function(e){
  console.log(e);
  }
 }

);

但我不知道如何将两个参数发送到控制器。感谢您的帮助。

【问题讨论】:

  • “数据”输出什么(隐藏输入)?
  • 这是一个如何将数据发送到控制器的示例。你可以有更多的对象,而不仅仅是“id”,同样的原则适用:stackoverflow.com/a/26413062/3036342

标签: ajax laravel-4


【解决方案1】:

你真的需要使用 GET 吗?更简洁的方法是使用 POST,如下所示:

public function postIndex()
{
    $arg1 = Request::input('argument1');
    $arg2 = Request::input('argument2');
    //etc...

    return Request::all();
}

ajax 调用:

            $.ajax({
                url: '{{ url('/test') }}',
                method: 'post',
                data: {
                    'argument1': argument1,
                    'argument2': argument2,
                    'argument3': argument3,
                    '_token': '{{ csrf_token() }}'
                },
                dataType: 'json'
            }).done(function (data) {
               .... do something with your returned data, if needed;
            }).fail(function(e){
                console.log(e); // gives your something to debug.
            });

【讨论】:

  • 是否正在使用理想的帖子从数据库中进行选择?对于 REST 规则?
【解决方案2】:

这并不总是理想的方式,但您可以随意添加参数:

url: '/test/' + data + '/' + foo + '/' + bar,

并像这样接收它们:

public function getIndex($data, $foo, $bar)
{
    return $data;
}

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2016-10-24
    • 2017-04-29
    • 2020-11-08
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2020-05-30
    相关资源
    最近更新 更多