- 论坛徽章:
- 0
|
在使用ajax时,一般我们传参数时会直接这样写
poststr = "&aboutus_en=" + document.form[0].elements.value;
send_request(url,"POST", poststr, function() {alert("hello");});数据是可以保存,但是如里document.form[0].elements.value中包含有特殊字符"&",那么保存到数据库中的数据会从这个字符后截断,因为web server会自动根据此字符来确定Querystring,如此一来,就有问题,这也是我们写代码时经常容易忽略的问题,为了解决此问题,只需要加用函数编码处理就OK,接收端不用处理,如果是用"GET"方法,那么要取得Querystring时在接收端需要用urldecode来确码(php中)处理.
下面是send_request原形:
var request;
function send_request(url,method,poststr,callbackfunc) {
request = new Initajax();
if (!request) {
window.alert("܄XMLHttpRequest");
return false;
}
request.open(method,url,true);
if (method=="POST") { request.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");}
request.onreadystatechange = callbackfunc;
if (method=="POST") { request.send(poststr);}
else { request.send(null);}
} //end send_request
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23633/showart_222264.html |
|