emu in blogjava

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  171 随笔 :: 103 文章 :: 1052 评论 :: 2 Trackbacks
* 用xmlhttp发送请求的时候,如果服务器返回的是2xx,4xx或者5xx状态,是可以从status属性获得状态码加以判断的,但是如果返回的是3xx状态(redirects),那么IE会自动跟随redirects转向到新的请求,xmlhttp无法拦截住这类status加以判断。所以用用Ajax技术玩http status要注意3xx头。
1xx类的状态(Intermediate)还没有碰到过,不是很关心。
* 服务器返回的http头里面如果带 Set-Cookie ,xmlhttp在接收的同时是可以自动设置cookie的。
posted on 2006-02-15 10:38 emu 阅读(1832) 评论(9)  编辑  收藏

评论

# re: AJAX的小经验,有个疑问 2006-02-16 12:34 漂漂
代码如下
function A(){
this.createRequest();
}
A.prototype.buildSoap = function(){
//创建一个soap,命名为this.soap
}
A.prototype.createRequest = function(){
//Use an XMLHttpRequest,命名为this.request
...
var url="...";
this.request.open("POST",url,true);
this.request.onreadystatechange= this.handleResponse;
this.buildSoap();
this.request.send(this.soap);
}
A.prototype.handleResponse= function(){
if (this.request.readyState == 4){
if (this.request.status == 200){
...
}
}
}
代码运行后,报错“this.request.readyState 为空或不是对象”,请问这种JS对象编程时,应该怎样来写?谢谢  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 13:02 emu
//Use an XMLHttpRequest,命名为this.request

这个双语合壁的注释都够莫名其妙了,实现代码还略过。看不出问题。  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 13:34 漂漂
呵呵,真不好意思-_-!,由于找不到你的其他联系方式,又怕写太多,就成了刚才那样子,完整的测试代码如下:

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<script language="javascript" type="text/javascript">
function startTest(){
var ob= new A();
ob.createRequest();
}
function A(){

}
A.prototype.buildSoap = function(){
//创建一个soap,命名为this.soap
var xmlString = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
"<SOAP:Header/>"+
"<SOAP:Body>"+
"<Authenticate>"+
"<username></username>"+
"<password></password>"+
"</Authenticate>"+
"</SOAP:Body>"+
"</SOAP:Envelope>";
this.soap = new ActiveXObject("Microsoft.XMLDOM");
this.soap.async = false;
this.soap.loadXML(xmlString);
this.soap.selectSingleNode(".//username").text = "administrator";
this.soap.selectSingleNode(".//password").text = "123456";
}
A.prototype.createRequest = function(){
//Use an XMLHttpRequest,命名为this.request
try {
this.request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
this.request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
this.request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
this.request = false;
}
}
}
if (!this.request)
alert("Error initializing XMLHttpRequest!");
var url="...";//访问地址,略
this.request.open("POST",url,true);
this.request.onreadystatechange= this.handleResponse;
this.buildSoap();
this.request.send(this.soap);
}
A.prototype.handleResponse= function(){
if (this.request.readyState == 4){
if (this.request.status == 200){
alert("服务完成");
}
}
}
</script>
<body onload="startTest()">
</body>
</HTML>
代码运行后,报错“this.request.readyState 为空或不是对象”,
其实碰到的问题主要是在处理onreadystatechange事件的函数上,由于该函数是我自己构造的对象A的函数,我不想把函数写成全局的。
  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 13:44 漂漂
用了双语合壁的注释,比较着急中,确实没在意这些问题,呵呵  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 14:17 emu
你都没有搞清楚原型对象和对象实例的差别,就this来this去的。

this.request.onreadystatechange= this.handleResponse;

handleResponse是原型对象上的方法,不是对象实例的方法,而this.request是对象实例里面的子对象。

你想要在handleResponse里面通过this引用对象实例,可是原型对象的方法里面的this指向的是原型对象而不是实例对象,request是实例对象的属性,原型对象没有request当然要报错了。

this.request.send(this.soap);
好好看看xmlhttp的api吧,有接受xmldom对象作为参数的send方法吗?
  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 14:41 emu
帮你改一个能运行的版本,但是具体的业务和功能我就不得而知了。
注意到你尝试创建非IE浏览器的 XMLHttpRequest 对象,可是buildSoap方法使用了Microsoft.XMLDOM对象已经注定了这个程序不能在非IE浏览器上运行了,写了也是白写,但是还是给你保留了。

<HTML>
<HEAD>
<TITLE> SOAP test by emu </TITLE>
<META NAME="Author" CONTENT="emu">
</HEAD>
<script language="javascript" type="text/javascript">
function startTest(){
  var ob= new A();
  ob.createRequest();
}
function A(){ }
A.prototype.buildSoap = function(){
  //创建一个soap,命名为this.soap
  var xmlString = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
  "<SOAP:Header/>"+
  "<SOAP:Body>"+
  "<Authenticate>"+
  "<username></username>"+
  "<password></password>"+
  "</Authenticate>"+
  "</SOAP:Body>"+
  "</SOAP:Envelope>";
  this.soap = new ActiveXObject("Microsoft.XMLDOM");
  this.soap.async = false;
  this.soap.loadXML(xmlString);
  this.soap.selectSingleNode(".//username").text = "administrator";
  this.soap.selectSingleNode(".//password").text = "123456";
}
A.prototype.createRequest = function(){
  var r;
  try {r=new XMLHttpRequest();}catch(e){}
  if(!r)  try {r=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
  if(!r)  try {r=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
  if (!r) {
    alert("Error initializing XMLHttpRequest!");
    return;
  }
  var url="http://www.blogjava.net";
  r.open("POST",url,true);
  r.onreadystatechange= function(){
    if(r.readyState==4&&r.status==200)
      alert("服务完成");
  };
  this.buildSoap();
  r.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
  r.setRequestHeader("If-Modified-Since","0");
  r.send(this.soap.xml);
}
</script>
<body onload="startTest()">
</body>
</HTML>
  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 14:47 漂漂
Send方法的参数类型是Variant,可以是字符串、DOM树或任意数据流。

看了你的解答后,修改了一下代码,程序运行达到了预期效果,但是感觉比较繁琐

将原来的“this.request.onreadystatechange= this.handleResponse; ”修改成“var aaa=this; this.request.onreadystatechange=function(){get(aaa)};”
增加一个全局函数:
function get(aaa){
aaa.handleResponse();
}
由于onreadystatechange响应函数不能带参数,所以不得不写成“function(){get(aaa)}”,而且这样可以把我需要的this保留并传递。

有没有什么更简洁的写法呢?谢谢:)  回复  更多评论
  

# re: AJAX的小经验 2006-02-16 14:58 漂漂
哦 或者写成“var aaa=this;this.request.onreadystatechange = function(){aaa.handleResponse()};”  回复  更多评论
  

# re: AJAX的小经验 2006-02-17 11:25 emu
如果不是真的需要继承重用和多态的话,我不建议使用js来进行“面向对象”编程。用“基于对象”的语言来做“基于对象”的设计和编码,感觉要比较自然些。
用原型对象模拟的面向对象开发,在实现多态和继承的时候和传统的面向对象语言始终有各种差异,开发的时候要对js的对象体系有充分的理解和保持非常清晰的死路。
也有一些很优秀的面向对象js框架,但是我还是担心,解决一个问题,带来更多的问题。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: