Ajax 表单与 Laravel 5 中的文件一起提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28658457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:59:49  来源:igfitidea点击:

Ajax form submit with file in Laravel 5

phpjqueryajaxlaravellaravel-5

提问by socm_

I am trying to implement fileupload in laravel 5. But firstly want to send data, for example, simple string. My form:

我正在尝试在 Laravel 5 中实现文件上传。但首先要发送数据,例如简单的字符串。我的表格:

    {!!Form::open(["url"=>"/photos", "method" => "post", "files"=>true, "onsubmit"=>"send();return false;"])!!}    
    {!!Form::text("title", null, ["class"=>"form-control"])!!}    
    {!!Form::submit("Send", ["class"=>"btn btn-primary", "id" => "submitForm"])!!}    
    {!!Form::close()!!} 

My send function

我的发送功能

function send() {
    var formData = new FormData(this);
    $.ajax({        url: "/photos",
                    method: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    cache: false,
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(data) {                   
                    }
                });
}

My route:

我的路线:

Route::post("/photos", "PhotosController@store");

My store function

我的店铺功能

public function store()
{
    $data = \Illuminate\Support\Facades\Request::all();
    return $data;
}

And I have an empty data! Via Formdata is nothing receiving, but if I change params from formData to {text: "Hola"}, so I see that in answer from server, but via text I cant upload photos in future. What am I doing wrong ? Why I receive empty formdata ? Thanks

我有一个空数据!通过 Formdata 没有任何接收,但是如果我将参数从 formData 更改为 {text: "Hola"},那么我在服务器的回答中看到了这一点,但是通过文本我以后无法上传照片。我究竟做错了什么 ?为什么我收到空的 formdata ?谢谢

回答by Bananam00n

Since I got here with the same problem and found a solution I thought I'd post it here.

因为我遇到了同样的问题并找到了解决方案,所以我想我会把它贴在这里。

First of all you will have to use var formData = $(this).serializeArray();to get your form data, instead of var formData = new FormData(this);.

首先,您必须使用var formData = $(this).serializeArray();来获取表单数据,而不是var formData = new FormData(this);.

According to the documention Laravel will automatically return a HTTP response with 422 status code including a JSON representation of the validation errors.

根据文档,Laravel 将自动返回带有 422 状态代码的 HTTP 响应,其中包括验证错误的 JSON 表示。

full ajax call:

完整的ajax调用:

//#ajax-form is the id of your form.
$("#ajax-form").submit(function(e)
    {
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");

        $.ajax(
            {
                url : formURL,
                type: "POST",
                data : postData,
                success:function(data)
                {
                    console.log(data);
                    //data: return data from server
                },
                error: function(data)
                {
                    console.log(data.responseJSON);
                    //in the responseJSON you get the form validation back.
                }
            });
        e.preventDefault(); //STOP default action
        e.unbind(); //unbind. to stop multiple form submit.
});

if you want to upload files through AJAX I suggest you read this. Hope this helps!

如果您想通过 AJAX 上传文件,我建议您阅读此内容。希望这可以帮助!

Happy coding :-)

快乐编码:-)