我的代码:
<form id="company">
<input type="text" name="name" />
<input type="tel" name="tel" />
<input type="button" id="send" name="send" value="send" />
</form>
<script>
##get Form id an create form data##
var testForm = document.getElementById('send');
testForm.onclick = function(event) {
var formData = new FormData(document.getElementById('company'));
##this is Ajax Method##
$.ajax({
url : './json/company.php',
method : 'POST',
data : formData,
timeout : 10,
dataType :'json',
success: function(data)
{
alert("Success");
}
});
}
</script>
错误:TypeError:FormData.constructor的参数1不是对象
答案 0 :(得分:0)
试试这个
data : JSON.stringify(formData)
答案 1 :(得分:0)
由于您的表单没有输入类型文件,因此您应该使用serialize函数
$(testForm).click(function(event) {
event.preventDefault();//don't forget to prevent the default click event from redirecting you from the page
$.ajax({
url : './json/company.php',
method : 'POST',
data : $('#company').serialize(),
timeout : 10,
dataType :'json',
success: function(data)
{
alert("Success");
}
});
});
答案 2 :(得分:0)
您错过了关键选项,jQuery正在尝试处理formData,它无法实现。
您必须添加processData: false
选项,并且应该避免发送contentType
var testForm = document.getElementById('send');
testForm.onclick = function(event) {
var formData = new FormData(document.getElementById('company'));
$.ajax({
url: './json/company.php',
method: 'POST',
data: formData,
processData: false,
contentType: false,
timeout: 10,
dataType: 'json',
success: function(data) {
alert("Success");
}
});
}
答案 3 :(得分:0)
为了在jquery中使用formdata,你必须设置正确的选项
var testForm = document.getElementById('send');
testForm.onclick = function(event) {
var formData = new FormData(document.getElementById('company'));
$.ajax({
url : './json/company.php',
type: "POST",
data : formData,
processData: false,
contentType: false,
success: function(data)
{
alert("Success");
},
error: function(jqXHR, textStatus, errorThrown){
//if fails
}
});
}