<script type="text/javascript">
$('#fileUpload').submit(function (evt) {
evt.preventDefault();
$.ajax({
url: "/Transport/GridUpload",
type: 'POST',
data: $("#fileUpload").serialize(),
success: function (response) {
alert("it Works");
}
});
});
</script>
我正在尝试通过Ajax发布表单,但它不起作用。
表格html:
@using (Html.BeginForm("GridUpload", "Transport", new { tipas = 1 }, FormMethod.Post, new {enctype="multipart/form-data", id = "fileUpload" }))
{
<table style="margin-top:0px;">
<tr>
<td>
<label for="file">Filename:</label>
</td>
<td>
<input type="file" name="file" id="file" />
</td>
<td>
<input type="submit" class="upload-button" value="Upload" />
</td>
</tr>
</table>
}
我收到错误:
POST http://localhost:3043/Transport/GridUpload 500 (Internal Server Error)
我的代码可能有什么问题?我错过了什么吗? 对于记录,如果我删除evt.preventDefault();一切正常,但我需要阻止在没有ajax的情况下提交表单。
答案 0 :(得分:0)
更改此
<input type="button" id="fileUpload" class="upload-button" value="Upload" />
$('#fileUpload').click(function (evt) {
更改
@using (Html.BeginForm("GridUpload", "Transport", new { tipas = 1 },
FormMethod.Post, new {enctype="multipart/form-data", id = "fileUpload1" }))