【问题标题】:Getting "too few arguments passed to function" when submitting a Laravel form with Ajax使用 Ajax 提交 Laravel 表单时获得“传递给函数的参数太少”
【发布时间】:2018-11-01 05:46:02
【问题描述】:

我正在 Bootstrap 模式中构建一个表单,当您按下“保存”按钮时,它会提交给 Laravel。

表单本身由一个文本字段、一个文本区域、一个文件输入和一个复选框组成。

应该接收所有数据的控制器现在如下所示:

/**
 * @param $request
 * @return \Illuminate\Http\JsonResponse
 */
public function postCategory($request) {
    return response()->json($request);
}

我现在的目标是确保一切都来自于好的控制器的操作,并以 JSON 字符串的形式返回到 jQuery。完成后,我将处理如何保存表单的内容。

这是我提交的 Ajax 的外观:

$("#new_category").submit(function(e) {

    e.preventDefault();

    $.ajax({
        type: "POST",
        url: window.location.origin + '/admin/categories',
        data: {
            data: $(this).serialize()
        },
        dataType: "json",
        success: function(data){
            console.log(data);
        }
    });

});

更新

这是表单的外观:

<form id="new_category">
    <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Nouvelle catégorie</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
    <div class="form-group">
        <label for="category_title">Titre</label>
        <input type="text" class="form-control" id="category_title" placeholder="Titre" name="category_title">
    </div>
    <div class="form-group">
        <label for="category_description">Description</label>
        <textarea class="form-control" name="category_description" id="category_description" rows="3" placeholder="Description..."></textarea>
    </div>
    <div class="form-group">
        <label for="category_illustration">Illustration</label>
        <input type="file" class="form-control-file" name="category_illustration" id="exampleFormControlFile1">
    </div>
    <div class="form-check">
        <input type="checkbox" name="category_enabled" class="form-check-input" id="category_enabled" checked="checked">
        <label class="form-check-label" for="category_enabled">Actif</label>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
    <button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
</form> 

这是我处理提交的路线:

Route::post('/admin/categories', 'HomeController@postCategory');

我还发现 this question 对我有很大帮助,但我仍然遇到这些问题。

我该怎么办?

提前谢谢你

【问题讨论】:

  • 你的路线是什么?
  • 这个错误意味着 - 被调用的函数需要 n 个参数,而你已经传递了 n-x 个参数。
  • 我更新了我的问题
  • @adi 是的,我明白这意味着什么,但我不明白为什么我得到它,它不应该发生(因此我的问题在这里)
  • 如果您像 Theodore B. 那样在控制器中键入 postCategory 方法会发生什么?

标签: jquery ajax laravel


【解决方案1】:

我会尝试将代码编辑成这样:

/**
 * @param $request
 * @return \Illuminate\Http\JsonResponse
 */
public function postCategory(Request $request) {
    return response()->json($request->all());
}

【讨论】:

  • 谢谢,不知道Javascript会发送Request对象,下次一定记得!
猜你喜欢
  • 2020-07-30
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2018-05-19
  • 1970-01-01
相关资源
最近更新 更多