的形式为:AJAX不张贴到的WebMethod
<form action="upload-document.aspx" onsubmit="sendAndClose();" method="post" enctype="multipart/form-data">
<input name="fileToUpload" id="fileToUpload" type="file" />
<input type="submit" name="submit" value="Send" />
</form>
的AJAX:
function sendAndClose() {
currentUrl = location.protocol + '//' + location.host + location.pathname;
var data = new FormData();
var file = $("#fileToUpload")[0].files[0];
data.append("name", file.name);
data.append("size", file.size);
data.append("type", file.type);
data.append("file", file);
$.ajax({
type: "POST",
url: currentUrl + '/Persist',
dataType: 'json',
data: data,
cache: false,
contentType: false,
processData: false,
success: function() {
parent.$.fancybox.close();
},
error: function (request, error) {
alert("[" + error + "] - FAIL: " + request.responseText);
parent.$.fancybox.close();
}
});
}
隐藏代码:
[WebMethod]
public static bool Persist(object data)
{
return true;
}
时提交表单,它运行AJAX并在进入webmethod之前直接进入错误回调。有人可以告诉我为什么吗?
另外,在'var file'之后,我有一个警告显示文件的名称,大小等等...所以它获取文件,问题是ajax拒绝与代码隐藏通讯。
如果您在请求中运行Fiddler,您会得到什么答复......? http://fiddler2.com/ –
将方法注释为'[HttpPost]'。 –
Calil:不使用mvc:theres没有HttpPostAttribute –