form表單準備如下
<form id="form">
@csrf
<input type="text" name="name">
<input type="password" name="password">
<input type="file" name="file">
<button type="button" onclick="store()">提交</button>
</form>
不包含檔案表單資料
function store() {
$.ajax({
url: '{{route('store')}}',
data: $('#form').serialize(),
type:'post',
success: function (res) {
}
})
}
function store() {
var formData = new FormData($('#form')[0])
$.ajax({
url: '{{route('store')}}',
data: formData,
contentType: false,
processData: false,
type:'post',
success: function (res) {
}
})
}
包含檔案表單資料
function store() {
var formData = new FormData($('#form')[0])
$.ajax({
url: '{{route('store')}}',
data: formData,
contentType: false,
processData: false,
type:'post',
success: function (res) {
}
})
}
Form Data 更多操作
var formData = new FormData();
var elment = document.querySelector("#form")
var elment = $("#form")[0]
var formData = new FormData(element);
formdata.append("k1","v1");
formdata.get("k1");
formdata.set("k1","v2");
if (formdata.has("k1")) {
}
formdata.delete("k1");
for(var k of formData.keys()){
console.log(formData.get(k))
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結