如何使用JavaScript和AJAX在JSP中提交表单?
我有一个表单,在这个表单中,我有多个字段,包括一个图像。基本上,我正在尝试提交包含其他字段的产品图像,并使用AJAX提交表单。但我面临这样做的问题。我不知道我哪里做错了。如果您有任何建议,将不胜感激如何使用JavaScript和AJAX在JSP中提交表单?,javascript,ajax,spring-boot,spring-mvc,jsp,Javascript,Ajax,Spring Boot,Spring Mvc,Jsp,我有一个表单,在这个表单中,我有多个字段,包括一个图像。基本上,我正在尝试提交包含其他字段的产品图像,并使用AJAX提交表单。但我面临这样做的问题。我不知道我哪里做错了。如果您有任何建议,将不胜感激 <form id="addproductform" name="addproductform" enctype="multipart/form-data"> <input type="button" c
<form id="addproductform" name="addproductform" enctype="multipart/form-data">
<input type="button" class="btn btn-outline-danger mt-4" value="Submit" onclick="addproduct()">
function addproduct(){
var productname = document.getElementById('productname').value;
var category = document.getElementById('categorydd').value;
var subcategory = document.getElementById('subcategorydd').value;
var tag = document.getElementById('tagsdropdown').value;
var skucode = document.getElementById('skucode').value;
var productimg = document.getElementById('imagepath').value;
var url = "../api/products";
console.log(url);
$.post(url,{
productname : productname,
category : category,
subcategory : subcategory,
tag : tag,
skucode : skucode,
productimg : productimg,
},function(data, status) {
if (data.status == "OK") {
if (data.statusCode == 1) {
$('#addproductform').submit();
}else {
var error = data.responseMessage;
swal(error, "", "error");
}
} else {
var error = data.responseMessage;
swal(error, "", "error");
}
});
}
函数addproduct(){
var productname=document.getElementById('productname')。值;
var category=document.getElementById('categorydd')。值;
var subcategory=document.getElementById('subcategory dd')。值;
var tag=document.getElementById('tagsdropdown')。值;
var skucode=document.getElementById('skucode')。值;
var productimg=document.getElementById('imagepath')。值;
var url=“../api/products”;
console.log(url);
$.post(网址:{
productname:productname,
类别:类别,,
子类别:子类别,
标签:标签,
skucode:skucode,
productimg:productimg,
},功能(数据、状态){
如果(data.status==“正常”){
如果(data.statusCode==1){
$('#addproductform')。提交();
}否则{
var错误=data.responseMessage;
swal(错误,“,”错误“);
}
}否则{
var错误=data.responseMessage;
swal(错误,“,”错误“);
}
});
}
那么,您到底面临什么问题?编辑:无需担心。尝试constdata=newformdata($('addproductform')[0]),而不是手动获取所有这些数据代码>。现在发送,如图所示:当我提交排除图像字段时,它工作正常。但在通过图像字段后,表单没有提交。