ajax获取表单内容并组成URL的优秀代码(可用来参考获取表单内容)

前端之家收集整理的这篇文章主要介绍了ajax获取表单内容并组成URL的优秀代码(可用来参考获取表单内容)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/异步提交表单:1.获取表单内容组成url 2.提交表单
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(); }

猜你在找的Ajax相关文章