使用escape和unescape函数配合使用
后台语言asp
<%
response.write(escape("中文测试"))
%>
页面js脚本(只写关键部分)
假设返回的resopnseText为value则
alert(unescape(value));
即可在页面获得正确的中文字符。
反过来,在页面js脚本里面
escape("args=中文字符")
在后台使用
unescape(request.("args"))
可以获取args的值
xmlHttpRequest创建调用函数如下:
/********************创建ajax异步调用函数*********************/
/*
使用这种方式创建的对象,必须先 new 出一个对象,然后调用对象实例的方法
var aj = new $ajax();
aj.sendPOST(...);
*/
window.$ajax = function(){
var version = "1.0";
return {
//被页面调用的POST方式函数
sendPOST : function(url, data, callback) {
return this.send(url, "POST", callback, data);
},
//被页面调用的GET方式函数
sendGET : function(url, callback, args) {
return this.send(url, "GET", callback, args);
},
send : function(url, method, callback, data) {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex){
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
}
//将onreadystatechange属性直接赋值给回调方法
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 400) {//获取页面并成功返回数据
(method=="POST") ? callback(xmlHttp) : callback(xmlHttp,data);
} else {
//什么也不做
}
}
}
//根据不同的请求方式
if (method=="POST") {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(data);
} else {
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
try
{
return xmlHttp;
}catch(e)
{ }
}
}
};