如何通过使用PATCH方法的jQuery ajax将包含文件的表单发送到Laravel 5? [英] How to send form containing files via jQuery ajax using PATCH method to Laravel 5?

查看:53
本文介绍了如何通过使用PATCH方法的jQuery ajax将包含文件的表单发送到Laravel 5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 //routes.php
Route::match(['post', 'patch'],'/slide/{id}', function() {
    dd(request()->all());
});

//form
<form id="form" enctype="multipart/form-data">
    <input type="text" name="title">
    <input type="file" name="image">
</form>

//js
$('#form').on('submit',(function(e) {

    $.ajax({

        type: "PATCH",
        url:'/slide/' + id,
        data: new FormData(this),
        cache: false,
        contentType: false,
        processData: false

    }).done(function(r) {

        console.log(r);

    });
}));

当我使用POST方法时,一切都很好,并且dd(request()-> all())返回:

When I use POST method everything is fine and dd(request()->all()) returns:

array:2 [
  "title" => "foo"
  "file" => UploadedFile {#400
    -test: false
    -originalName: "bar.png"
    -mimeType: "image/png"
    -size: 4413
    -error: 0
    ...
  }
]

但是当更改PATCH的方法时,我收到了空数组.

but when change method to PATCH i receive empty array.

有人可以解释我在做什么错,并分享使用PATCH方法通过Ajax向L5发送FormData的正确方法吗?

Can someone explain what am I doing wrong and share proper way of sending FormData via ajax using PATCH method to L5?

我正在使用Laravel 5.2和jQuery 2.2.3

Im using Laravel 5.2 and jQuery 2.2.3

推荐答案

您可以执行此名为方法欺骗的操作.这个技巧就是Laravel的形式.如果您想发送补丁请求,可以在代码中加入这一行.

You can do this named method spoofing. this trick is what Laravel does in its forms. if you want to send patch request you can put this line in your code.

<input type="hidden" name="_method" value="PUT">

您可以在此处详细了解方法欺骗.

you can read more about method spoofing here.

这篇关于如何通过使用PATCH方法的jQuery ajax将包含文件的表单发送到Laravel 5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆