【发布时间】:2016-09-18 16:25:22
【问题描述】:
首先,请原谅我的英语。我正在尝试使用 ajax 提交表单,我的 js 代码是下一个:
$("#formPublicidad").on('submit', function(event) {
event.preventDefault();
var dataForm = new FormData(document.getElementById('formPublicidad'));
if(dataForm.get('url') == '' || dataForm.get('texto') == '' || dataForm.get('imagen') == '') {
$("#formMsg").text('Debe rellenar todos los campos');
$("#formMsg").css('display', 'block');
}else {
$("#formMsg").text('Cargando...');
$(".btn-form-pub").attr('disabled', true);
$.ajax({
url: 'publicidad.ajax.php',
method: 'POST',
data: dataForm,
pocessData: false,
contentType: false,
cache: false,
dataType: 'json',
success: function(response) {
var msg = JSON.stringify(response.msg).replace(/\"/g, "");
$(".btn-form-pub").attr('disabled', false);
if(msg != 'success') {
document.getElementById('formMsg').textContent = msg;
document.getElementById('formMsg').style = 'display: block;';
}else {
$("#formModal").modal('hide');
}
}
});
}
});
这是我的 HTML 代码:
<form method="post" id="formPublicidad">
<div class="modal-body">
<span id="formMsg"></span>
<input type="hidden" name="posicion" id="pubPos">
<label for="publiUrl">URL</label>
<input type="text" name="url" id="publiUrl" placeholder="Ej.: www.google.com.uy" class="form-control">
<label for="publiText">Texto</label>
<input type="text" name="texto" id="publiText" placeholder="Ej.: Google" class="form-control">
<label for="publiImg">Imagen</label>
<input type="file" name="imagen" id="publiImagen">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-form-pub" data-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-primary btn-form-pub" id="btn-add-pub">Guardar cambios</button>
</div>
</form>
当我尝试在控制台中通过 ajax 提交时,出现下一个错误:
TypeError:在未实现 FormData 接口的对象上调用了“append”。
我已经做过很多次了,但现在不行了。 请问有人可以帮忙吗?非常感谢!!
【问题讨论】:
-
可能无法解决您的问题。但我可以建议改用required 属性吗
-
感谢您提供所需的属性! Evan y 看到了这些可能的重复项,但我已将 contentType 和 processData 设置为 false...
标签: javascript jquery ajax