【问题标题】:Missing argument error when posting data through ajax (using Laravel)通过 ajax 发布数据时缺少参数错误(使用 Laravel)
【发布时间】:2013-06-11 18:47:39
【问题描述】:

我会准确地说:$.post jQuery 函数和我的控制器中的一个动作有一个奇怪的问题。我是 PHP 新手,但我以前使用过 ASP.NET MVC,我希望它们是相似的。问题来了:

//This is my ajax call
$.post('get_random_photos', count, function(data) {
    ...
});

//This is my action
public function post_get_random_photos($count) {
    ...
}

//This is what I have in my routes.php
Route::post('photos/get_random_photos', 'photos@get_random_photos');

当我发出 ajax 请求时,我收到以下消息:

Missing argument 1 for Photos_Controller::post_get_random_photos()

我知道这可能可以使用Input::get('count'); 解决,但我宁愿将count 作为操作参数,就像我以前在 ASP.NET MVC 中所做的那样。有没有办法做到这一点?

【问题讨论】:

    标签: php jquery post laravel laravel-3


    【解决方案1】:

    您的宁静路线需要在 url 中接收该计数,这是这样做的一种方式:

    //This is my ajax call
    $.post('get_random_photos/'+count, 'nothing', function(data) {
        ...
    });
    
    //This is my action
    public function random_photos($count) {
        ...
    }
    
    //This is what I have in my routes.php
    Route::post('photos/get_random_photos/(:num)', 'photos@random_photos');
    

    【讨论】:

    • 是的,我可以这样做,但我需要我的 ajax 来发帖,有什么解决方案吗?
    • 实际上,如果您不需要传递比计数更多的值,则只需在 post 上传递一个虚拟值。或者,正如您所说,也许使用 Input::get('count')。已编辑。
    • 几乎可以,我需要将{count} 更改为(:num),请修正您的答案以供进一步参考,我会接受。
    • 已编辑并标记为 Laravel 3,我总是忘记询问哪个版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2015-09-26
    • 2017-10-20
    相关资源
    最近更新 更多