function submitFormDataAsyn(form) {
var xhr;
if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
return null;
}
}
var method = form.method ? form.method.toUpperCase() : defaultSubmitMode;
var action = form.action ? form.action : document.URL;
var data = getFormData(form);
var url = action;
if (data && method == "GET") {
url += "?" + data;
}
xhr.open(method,url,true);
function submitCallback() {
if (xhr.readyState == 4 && xhr.status != 200) {
alert("Auto-Save Error: " + xhr.status + " " + xhr.statusText);
}
}
xhr.onreadystatechange = submitCallback;
xhr.setRequestHeader("Ajax-Request","Auto-Save");
if (method == "POST") {
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(data);
} else {
xhr.send(null);
}
return xhr;
}
//同步提交表单,如果成功提交并且后台操作成功则返回<id>id</id>,否则返回错误信息
function submitFormData(form) {
var method = form.method ? form.method.toUpperCase() : defaultSubmitMode;
var action = form.action ? form.action : document.URL;
var data = getFormData(form);
var url = action;
if (data && method == "GET") {
url += "?" + data;
}
return ajaxSubmit(method,data);
}
function ajaxSubmit(method,data,returnxml)
{
var xhr;
if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
return "Not support ajax!";
}
}
xhr.open(method,false);
xhr.setRequestHeader("Ajax-Request","application/x-www-form-urlencoded");
xhr.send(data);
} else {
xhr.send(null);
}
return xhr;
}
function getParam(name,value) { return encodeURI(name).replace(/\+/g,"%2B") + "=" + encodeURI(value ? value : "").replace(/\+/g,"%2B"); } function postFormByAjax(url,nameOfFormToPost,callBackFunction) { //get the (form based) params to push up as part of the get request if(nameOfFormToPost != null && nameOfFormToPost!="") { //url=url+getFormAsString(nameOfFormToPost); var theForm=document.getElementsByName(nameOfFormToPost)[0]; theForm.action=url; theForm.method="POST"; var method = theForm.method ? theForm.method.toUpperCase() : defaultSubmitMode; var action = theForm.action ? theForm.action : document.URL; var data = getFormData(theForm); var url2 = action; if (data && method == "GET") { url2 += "?" + data; } ajaxSubmit2(method,url2,callBackFunction); } } function ajaxSubmit2(method,callBackFunction) { var xhr; if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } else { if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { return "Not support ajax!"; } } if (xhr) { if (callBackFunction != null) __callbackFunction = callBackFunction; xhr.onreadystatechange = __processStateChange; } xhr.open(method,true); //xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.send(); }
原文链接:https://www.f2er.com/ajax/164456.html