<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>xmltest</title>
<meta http-equiv="content-type" content="text/html;utf-8">
</head>
<script language="javascript">
//得到Ajax对象
function getAjax(){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch(e){
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
return xmlHttp;
}
//发送Ajax请求
function sendHttp(){
var xmlHttpAjax=getAjax();
var paramevalue=createxml();
var url="vodadi";
//(true(异步)|false(同步))
xmlHttpAjax.open("post", url, true);
xmlHttpAjax.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
//发送带参数的请求,如果不带参数,括号中就不写值
xmlHttpAjax.send("xmlParame="+paramevalue);
//XMLHttpRequest的状态在自动切换过程中会自动调用该方法
xmlHttpAjax.onreadystatechange = function(){
//当该对象的状态切换到4并且服务器端成功返回一个200状态码的时候,可以获得服务器端返回的数据
//4是XMLHttpRequest本身的一个状态码,200是服务器返回来的状态码
if(xmlHttpAjax.readyState == 4 && xmlHttpAjax.status == 200) {
handler(xmlHttpAjax.responseText);
}
};
}
//xml参数
function createxml(){
//"/"这不是大写V而是转义是左斜杠和右斜杠
var xml="<Object><Name><AssetID>EGEZ0595967209325440</AssetID><ProviderID>so.xor-media.com</ProviderID></Name><SubType>index</SubType></Object>";
return xml;
}
//发送请求成功后调用的方法
function handler(data){
var paramelable= document.getElementById("paramediv");
paramelable.innerHTML=data;
}
//js和jQuery的$(“#mfomr”). Serialize()相等的方法如下
function serialize(formID){
var form;
if (typeof formID == "object") {
form = formID;
}
else {
form = $(formID);
}
var param = "";
for (var i = 0; i < form.length; i++) {
if (form[i].type == "radio" || form[i].type == "checkbox") {
if (!form[i].checked) {
continue;
}
}
if (form[i].type == "submit") {
continue;
}
var name = form[i].name;
var val = form[i].value;
param += name + "=" + val + "&";
}
return param.substr(0, param.length - 1);
}
</script>
<body>
<input type="button" value="发送http请求" onclick="sendHttp()">
<label id="paramediv" style="color:red;"></label>
</body>
</html>
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent