【发布时间】:2013-08-28 09:44:08
【问题描述】:
我有以下格式的代码。
PHP 文件:
<form action="http://clientwebapi.com/createEvent" id="form_createEvent" method="post" enctype="multipart/form-data">
<input type="text" name="image_title" />
<input type="file" name="media" accept="image/*,video/*"/>
</form>
JQUERY 文件:
$('#form_createEvent').submit(function () {
var form = $(this);
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
xhrFields: {
withCredentials: true
},
data: form.serialize()
}).done(function () {
showCurrentLocation();
alert('Event created successfully..');
location.reload();
}).fail(function () {
alert("fail!");
});
event.preventDefault();
});
以上 Jquery 代码正在提交中。此外,当我提交以下格式时,它将重定向到“http://clientwebapi.com/createEvent”并成功创建事件。
表单提交并重定向到客户端页面:
$('#form_createEvent').submit(function () {
var fd = new FormData();
fd.append('media', input.files[0]);
$.ajax({
url: form.attr("action"),
data: fd,
processData: false,
contentType: false,
type: form.attr("method"),
success: function (data) {
alert(data);
}
});
event.preventDefault();
});
在此处提交表单并使用给定图像创建事件时如何防止。请帮忙
【问题讨论】:
-
在
$.ajax()之后最后做return false; -
实际上我正在将包括图像在内的数据发送到“clientwebapi.com/createEvent”。处理完之后,它会发送一些响应。我需要知道我们如何通过这个 jquery、ajax 代码将图像传递给客户端 API
标签: php jquery ajax multipartform-data