rails ajax,Rails Ajax 成功使用xmlHttpRequest

用了rails后,好久没有用xmlHttpRequest对象了,因为rails把xmlHttpRequest给封装了,rails自带了很多ajax方法,今天用了下xmlhttprequest,还真遇到不少麻烦

测试通过:

opera 9.6 + IE 6.0 + FF 3 + chrome 2

html:

javascript:

function createXmlHttpRequest(){//创建对象try{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}catch(e){try{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}catch(e2){

xmlHttp=false;

}

}if(!xmlHttp&&typeof XMLHttpRequest!="undefined")

{

xmlHttp=new XMLHttpRequest();

}

}

function checkLogin(node){//html中触发的js函数

var loginname=document.getElementById('ctl00_main_content_txtLogin').value;

createXmlHttpRequest();

xmlHttp.open("Get","/home/response_validate?login="+loginname+"&ts="+new Date().toString(),true);

xmlHttp.send(null);

xmlHttp.onreadystatechange=processor;//注意这里processor不能有括号

}

function processor(){//回调函数if(xmlHttp.readyState==4||xmlHttp.readyState=='complete'){//注意S要大写if(xmlHttp.status==200){

var responsetext=xmlHttp.responseText;if((responsetext.toString())=="true"){

loginError=document.getElementById("valstatus").innerHTML='该登录名已经被使用';

}else{

alert("测试");

}

}

}

}

服务器端:

defresponse_validate

@login_name=params[:login]

@value=true

render :partial=>'response_validate'end

partial:

总结:

1,rails中服务器返回text数据,通过render的方式,不想java中是通过 out.println 返回的

2,xmlHttp.onreadystatechange = processor;这里的processor后不能有(),不知道为什么,不然只能返回到1

3,readyState 的s必须大写,不然在opera和ff中会有bug的

4,数据改变为string,js是通过toString()方法,例如 responsetext.toString()

5,为了防止缓存问题可以再发送请求的时候加个随机的数据,例如上面的 xmlHttp.open("Get","/home/response_validate?login="+loginname+"&ts="+new Date().toString(),true);

ref:

write by feng

posted on 2009-05-08 18:54 fl1429 阅读(756) 评论(0)  编辑  收藏 所属分类: Ajax

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值