【问题标题】:error 422 (Unprocessable Entity) in laravellaravel 中的错误 422(无法处理的实体)
【发布时间】:2023-03-22 06:21:01
【问题描述】:

我在 Laravel 中遇到问题,因为它显示错误。我正在使用 ajax 将请求从客户端发送到服务器并向后发送。这是我的 AJAX 代码,我怀疑函数 append() 不起作用。

$('#petition-form').on('submit', function(e) {
     e.preventDefault();
     var formdata = new FormData($(this)[0]);
     formdata.append('content', tinymce.get('content').getContent());
     $.ajax({
         url: $(this).attr('action'),
         data: formdata,
         processData: false,
         contentType: false,
         cache: false,
         method: 'POST'
     }).done(function(response) {
         window.location.href = response.slug;
     }).fail(function(response) {
         $(this).append(parseJSON(response));
         // var getError = $.parseJSON(response);
     });
}):

当我尝试console.log(response) 时,它会返回一个数组。 你能帮我解决这个问题吗?

【问题讨论】:

  • 我遇到了同样的错误。但是在请求头中添加 Content-Type: application/json 解决了这个问题。

标签: php ajax laravel-5


【解决方案1】:

如果你想获取所有数据,那么你可以使用 serialize()

$.ajax({
    ...
    data: $(this).serialize(),
    ...
})

在控制器中你可以得到所有这样的细节

$data = $request->all();

其中$requestRequest 的一个实例

this post 也会帮助你

我希望这行得通。

【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题,我只是将 mce 作为我的输入“代理”,然后有一个具有正确名称的隐藏文本字段,然后:

    function reply(){   
      $("#message").html("Sending.....");
      $("#reply").attr("value",tinymce.get('reply-proxy').getContent());
    
      $.ajax( {
        url: '{{route("reply_conversation")}}',
        type: 'POST',
        data: new FormData( $("#reply_form")[0] ),
    

    也许不是最干净的工作,但它完成了工作:/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 2014-12-24
      • 2016-04-30
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多