Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1691891
  • 博文数量: 410
  • 博客积分: 9563
  • 博客等级: 中将
  • 技术积分: 4517
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-03 19:59
个人简介

文章分类

全部博文(410)

文章存档

2017年(6)

2016年(1)

2015年(3)

2014年(4)

2013年(32)

2012年(45)

2011年(179)

2010年(140)

分类: Python/Ruby

2011-01-23 12:01:43

用GET方法提交一个xml字符串
  1. var xmlHttp;
  2. function createXMLHttpRequest() {
  3.     if (window.ActiveXObject) {
  4.           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  5.     } else if (window.XMLHttpRequest) {
  6.           xmlHttp = new XMLHttpRequest();
  7.     }
  8. };
  9. function startRequest(xmlstr) {
  10.     createXMLHttpRequest();
  11.     xmlHttp.onreadystatechange = handleStateChange;
  12.     var localurl=document.location.href;
  13.     //如果末尾有#去掉
  14.     if (localurl.charAt(localurl.length-1)=="#"){
  15.     localurl=localurl.substring(0,localurl.length-1)}
  16.     var qurl = localurl+"../../../ajax.ks/ajax?xmlstr="+xmlstr;
  17.     //发送请求到的URL地址
  18.     xmlHttp.open("GET", qurl, true);
  19.     xmlHttp.send();
  20. };
function handleStateChange() {
  1.     if(xmlHttp.readyState == 4) {
  2.         if(xmlHttp.status == 200) {
  3.             res=xmlHttp.responseText;
  4.             //alert(res);
  5.             if (res!="ok\n"){
  6.                 alert("操作不成功,请刷新页面");
  7.             }
  8.          }
  9.     }
  10. };
用POST方法提交
  1. var xmlHttp;
  2. function createXMLHttpRequest() {
  3.     if (window.ActiveXObject) {
  4.           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  5.     } else if (window.XMLHttpRequest) {
  6.           xmlHttp = new XMLHttpRequest();
  7.     }
  8. };
  9. function startRequest(xmlstr) {
  10.     createXMLHttpRequest();
  11.     xmlHttp.onreadystatechange = handleStateChange;
  12.     var localurl=document.location.href;
  13.     //如果末尾有#去掉
  14.     if (localurl.charAt(localurl.length-1)=="#"){
  15.     localurl=localurl.substring(0,localurl.length-1)}
  16.     var postqur= localurl+"../../../ajax.ks/ajax/";
  17.     var poststr= "xmlstr="+encodeURIComponent(xmlstr);
  18.     //发送请求到的URL地址
  19.     xmlHttp.open("POST", postqur, true);
  20.     xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  21.     xmlHttp.send(poststr);
  22. };
  23. function handleStateChange() {
  24.     if(xmlHttp.readyState == 4) {
  25.         if(xmlHttp.status == 200) {
  26.             res=xmlHttp.responseText;
  27.             //alert(res);
  28.             if (res!="ok\n"){
  29.                 alert("操作不成功,请刷新页面");
  30.             }
  31.          }
  32.     }
  33. };
区别:
  1.     //如果末尾有#去掉
  2.     if (localurl.charAt(localurl.length-1)=="#"){
  3.         localurl=localurl.substring(0,localurl.length-1)}
  4. -   var qurl = localurl+"../../../ajax.ks/ajax?xmlstr="+xmlstr;
  5. +   var postqur= localurl+"../../../ajax.ks/ajax/";
  6. +   var poststr= "xmlstr="+encodeURIComponent(xmlstr);
  7.     //发送请求到的URL地址
  8. -   xmlHttp.open("GET", qurl, true);
  9. -   xmlHttp.send();
  10. +   xmlHttp.open("POST", postqur, true);
  11. +   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  12. +   xmlHttp.send(poststr);
  13.     };
  14.     function handleStateChange() {
  15.        if(xmlHttp.readyState == 4) {
ps:
  1. get和post的参数位置不同
  2. post的数据要用encodeURIComponent()进行编码,否则Karrigell服务器端会报错
  3. 文件头要标明urlencoded格式
  4. 服务器端脚本不需要变化
阅读(848) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~