我试图通过创建ajax调用从aspx页面调用aspx.cs中的方法。然而,响应为0.一些研究引导我验证ajax调用中的数据类型和数据,但没有一个解决了问题。是否会有一个窗口失去焦点,这可能导致调用是UNSENT?
<script>
function blah(e) {
var url = 'Default.aspx/Test';
selectedFiles = document.getElementById("MainContent_file1");
var file = selectedFiles.files[0];
var reader = new FileReader();
reader.onload = function (e) {
var encrypted = CryptoJS.AES.encrypt(reader.result, '12345');
encrypted = encodeURIComponent(encrypted);
var encryptedFile = new File([encrypted], file.name + '.encrypted', { type: "text/plain", lastModified: new Date() });
var formData = new FormData();
formData.append('myFile', encryptedFile)
//data.append('file[0]', encryptedFile);
debugger;
$.ajax({
type: "POST",
data: {'aa': encryptedFile},
//data: encrypted,
cache: false,
contentType: false,
dataType: 'json',
processData: false,
url: "Default.aspx/Test",
success: function (response) {
debugger;
alert(response);
},
error: function (xhr, textStatus, error) {
debugger;
alert(xhr.statusText);
}
});
};
reader.readAsDataURL(file);
}
</script>
ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod()]
public static string Test(object aa)
{
return "Hello";
}
错误讯息:
xhr = Object {readyState: 0, responseText: "", status: 0, statusText: "error"}, textStatus = "error", error = ""
详细回复:Default.aspx:
Request URL:http://localhost:51936/Default.aspx
Request Method:POST
Status Code:200 OK
Cache-Control:private
Connection:Close
Content-Length:6169
Content-Type:text/html; charset=utf-8
Date:Fri, 09 Dec 2016 16:15:52 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
测试:
Request URL:http://localhost:51936/Default.aspx/Test
Accept:application/json, text/javascript, */*; q=0.01
Content-Type:text/plain;charset=UTF-8
Origin:http://localhost:51936
Referer:http://localhost:51936/Default.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
X-Requested-With:XMLHttpRequest