我试图在不重新加载页面的情况下发送一些表单,并且我试图了解幕后细节,因此不使用任何 JavaScript 库:
var http = createRequestObject();
function createRequestObject() {
var objAjax;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
objAjax=new XMLHttpRequest();
}
else
{// code for IE6, IE5
objAjax=new ActiveXObject("Microsoft.XMLHTTP");
}
return objAjax;
}
function display_progress() { ... }
function upload_file() {
var request = 'UploaderServlet';
http.open('post', request);
http.onreadystatechange = display_progress;
http.send(null); // HERE PROBABLY THE DATA SHOULD BE SENT
}
<form enctype="multipart/form-data" id="upload_form" name="upload_form" method="POST" action="UploaderServlet" onsubmit="upload_file(); return false;" target="upload_target">
Choose a file <br />
<input name="file" size="27" type="file" id="file" /> <br/>
<input type="submit" name="uploadSubmitButton" value="Upload" /><br />
<br />
</form>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
调用了upload_file(),但如果我做对了,则不会发送数据。请就发送数据的正确方式提出建议。