I am trying to implement fileupload in laravel 5. But firstly want to send data, for example, simple string. My form:
我试图在laravel 5中实现fileupload。但首先要发送数据,例如,简单的字符串。我的表格:
{!!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是没有收到的,但是如果我将params从formData更改为{text:“Hola”},那么我在服务器的回答中看到,但是通过文本我将来无法上传照片。我究竟做错了什么 ?为什么我收到空的formdata?谢谢
1 个解决方案
#1
4
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 :-)
快乐的编码:-)
#1
4
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 :-)
快乐的编码:-)