Javascript 每次我尝试保存表单时,XMLHttpRequest都会发送空表单数据
我遇到了一个问题,我试图保存表单,但每次它都向服务器发送空数据。 这仅在Windows Chrome浏览器上发生。在Mac Chrome上,它工作得非常好Javascript 每次我尝试保存表单时,XMLHttpRequest都会发送空表单数据,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我遇到了一个问题,我试图保存表单,但每次它都向服务器发送空数据。 这仅在Windows Chrome浏览器上发生。在Mac Chrome上,它工作得非常好 try { if (typeof XMLHttpRequest.prototype.sendAsBinary == 'undefined') { XMLHttpRequest.prototype.sendAsBinary = function(text){ var data = new ArrayBuffer(text
try {
if (typeof XMLHttpRequest.prototype.sendAsBinary == 'undefined') {
XMLHttpRequest.prototype.sendAsBinary = function(text){
var data = new ArrayBuffer(text.length);
var ui8a = new Uint8Array(data, 0);
for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);
this.send(ui8a);
};
}
} catch (e) {}
var AJAXSubmit = (function () {
function ajaxSuccess () {
/* console.log("AJAXSubmit - Success!"); */
alert("Saved successfully");
/* you can get the serialized data through the "submittedData" custom property: */
//alert(JSON.stringify(this.submittedData));
}
function ajaxError() {
alert("Sorry, there was an error. Please save again.");
}
function submitData (oData) {
/* the AJAX request... */
var oAjaxReq = new XMLHttpRequest();
console.log(oAjaxReq);
oAjaxReq.submittedData = oData;
oAjaxReq.timeout = 10000;
oAjaxReq.ontimeout = ajaxError;
oAjaxReq.onerror = ajaxError;
if (oData.technique === 0) {
/* method is GET */
oAjaxReq.open("get", oData.receiver.replace(/(?:\?.*)?$/, oData.segments.length > 0 ? "?" + oData.segments.join("&") : ""), true);
oAjaxReq.send(null);
} else {
/* method is POST */
oAjaxReq.open("post", oData.receiver, true);
if (oData.technique === 3) {
/* enctype is multipart/form-data */
var sBoundary = "---------------------------" + Date.now().toString(16);
//oAjaxReq.setRequestHeader("Content-Type", "multipart\/form-data; boundary=" + sBoundary);
oAjaxReq.sendAsBinary("--" + sBoundary + "\r\n" + oData.segments.join("--" + sBoundary + "\r\n") + "--" + sBoundary + "--\r\n");
oAjaxReq.onreadystatechange = function(){
if(oAjaxReq.readyState==4 && oAjaxReq.status ==200 && oAjaxReq.response == "1"){
oAjaxReq.onload = ajaxSuccess;
}
if(oAjaxReq.readyState==4 && oAjaxReq.status ==200 && oAjaxReq.response == "2"){
oAjaxReq.onload = ajaxError;
}
if(oAjaxReq.readyState!=4 && oAjaxReq.status !=200){
oAjaxReq.onload = ajaxError;
}
};
} else {
/* enctype is application/x-www-form-urlencoded or text/plain */
oAjaxReq.setRequestHeader("Content-Type", oData.contentType);
oAjaxReq.send(oData.segments.join(oData.technique === 2 ? "\r\n" : "&"));
}
}
}
function processStatus (oData) {
if (oData.status > 0) { return; }
/* the form is now totally serialized! do something before sending it to the server... */
/* doSomething(oData); */
/* console.log("AJAXSubmit - The form is now serialized. Submitting..."); */
submitData (oData);
}
function pushSegment (oFREvt) {
this.owner.segments[this.segmentIdx] += oFREvt.target.result + "\r\n";
this.owner.status--;
processStatus(this.owner);
}
function plainEscape (sText) {
/* how should I treat a text/plain form encoding? what characters are not allowed? this is what I suppose...: */
/* "4\3\7 - Einstein said E=mc2" ----> "4\\3\\7\ -\ Einstein\ said\ E\=mc2" */
return sText.replace(/[\s\=\\]/g, "\\$&");
}
function SubmitRequest (oTarget) {
var nFile, sFieldType, oField, oSegmReq, oFile, bIsPost = oTarget.method.toLowerCase() === "post";
/* console.log("AJAXSubmit - Serializing form..."); */
//console.log(oTarget);
this.contentType = bIsPost && oTarget.enctype ? oTarget.enctype : "application\/x-www-form-urlencoded";
this.technique = bIsPost ? this.contentType === "multipart\/form-data" ? 3 : this.contentType === "text\/plain" ? 2 : 1 : 0;
this.receiver = oTarget.action;
this.status = 0;
this.segments = [];
var fFilter = this.technique === 2 ? plainEscape : escape;
for (var nItem = 0; nItem < oTarget.elements.length; nItem++) {
oField = oTarget.elements[nItem];
if (!oField.hasAttribute("name")) { continue; }
sFieldType = oField.nodeName.toUpperCase() === "INPUT" ? oField.getAttribute("type").toUpperCase() : "TEXT";
if (sFieldType === "FILE" && oField.files.length > 0) {
if (this.technique === 3) {
/* enctype is multipart/form-data */
for (nFile = 0; nFile < oField.files.length; nFile++) {
oFile = oField.files[nFile];
oSegmReq = new FileReader();
/* (custom properties:) */
oSegmReq.segmentIdx = this.segments.length;
oSegmReq.owner = this;
/* (end of custom properties) */
oSegmReq.onload = pushSegment;
this.segments.push("Content-Disposition: form-data; name=\"" + oField.name + "\"; filename=\""+ oFile.name + "\"\r\nContent-Type: " + oFile.type + "\r\n\r\n");
this.status++;
oSegmReq.readAsBinaryString(oFile);
}
} else {
/* enctype is application/x-www-form-urlencoded or text/plain or method is GET: files will not be sent! */
for (nFile = 0; nFile < oField.files.length; this.segments.push(fFilter(oField.name) + "=" + fFilter(oField.files[nFile++].name)));
}
} else if ((sFieldType !== "RADIO" && sFieldType !== "CHECKBOX") || oField.checked) {
/* field type is not FILE or is FILE but is empty */
this.segments.push(
this.technique === 3 ? /* enctype is multipart/form-data */
"Content-Disposition: form-data; name=\"" + oField.name + "\"\r\n\r\n" + oField.value + "\r\n"
: /* enctype is application/x-www-form-urlencoded or text/plain or method is GET */
fFilter(oField.name) + "=" + fFilter(oField.value)
);
}
}
processStatus(this);
}
return function (oFormElement) {
if (!oFormElement.action) { return; }
new SubmitRequest(oFormElement);
};
})();
试试看{
if(XMLHttpRequest.prototype.sendAsBinary的类型=='undefined'){
XMLHttpRequest.prototype.sendAsBinary=函数(文本){
var数据=新数组缓冲区(text.length);
var ui8a=新的Uint8Array(数据,0);
对于(var i=0;i0?“+oData.segments.join(&”):”),true);
oAjaxReq.send(空);
}否则{
/*方法是POST*/
oAjaxReq.open(“post”,oData.receiver,true);
if(oData.technology==3){
/*enctype是多部分/表单数据*/
var sBoundary=“------------------------------------”+Date.now().toString(16);
//oAjaxReq.setRequestHeader(“内容类型”、“多部分\/表单数据;边界=“+sBoundary”);
oAjaxReq.sendAsBinary(“--”+sBoundary+“\r\n”+oData.segments.join(“--”+sBoundary+“\r\n”)+“--”+sBoundary+“--\r\n”);
oAjaxReq.onreadystatechange=函数(){
if(oAjaxReq.readyState==4&&oAjaxReq.status==200&&oAjaxReq.response==1”){
oAjaxReq.onload=ajaxSuccess;
}
if(oAjaxReq.readyState==4&&oAjaxReq.status==200&&oAjaxReq.response==2”){
oAjaxReq.onload=ajaxError;
}
if(oAjaxReq.readyState!=4&&oAjaxReq.status!=200){
oAjaxReq.onload=ajaxError;
}
};
}否则{
/*enctype为application/x-www-form-urlencoded或text/plain*/
oAjaxReq.setRequestHeader(“内容类型”,oData.contentType);
oAjaxReq.send(oData.segments.join(oData.technology==2?\r\n):“&”);
}
}
}
功能进程状态(oData){
如果(oData.status>0){return;}
/*表单现在已完全序列化!请在将其发送到服务器之前执行某些操作*/
/*doSomething(oData)*/
/*log(“AJAXSubmit-表单现在已序列化。正在提交…”)*/
提交数据(oData);
}
功能推送段(oFREvt){
this.owner.segments[this.segmentIdx]+=ofreft.target.result+“\r\n”;
this.owner.status--;
processStatus(this.owner);
}
函数plainEscape(sText){
/*我应该如何对待文本/纯格式编码?哪些字符是不允许的?这就是我所想的…:*/
/*“4\3\7-爱因斯坦说E=mc2”-->“4\\3\\7\-\Einstein\said\E\=mc2”*/
返回sText.replace(/[\s\=\\]/g,\\$&);
}
函数SubmitRequest(oTarget){
变量nFile,sFieldType,oField,oSegmReq,oFile,bIsPost=oTarget.method.toLowerCase()=“post”;
/*log(“AJAXSubmit-序列化表单…”)*/
//console.log(oTarget);
this.contentType=bIsPost&&oTarget.enctype?oTarget.enctype:“应用\/x-www-form-urlencoded”;
this.technical=bIsPost?this.contentType==“多部分\/表单数据”?3:this.contentType==“文本\/普通”?2:1:0;
this.receiver=oTarget.action;
这个状态=0;
这是。段=[];
var fFilter=this.technology==2?简单转义:转义;
对于(var nItem=0;nItem0){
if(this.technology==3){
/*enctype是多部分/表单数据*/
对于(nFile=0;nFile