代码如下:
<script language='javascript'>
var http_request = false;
function send_request(method,url,content,responseType,callback){
http_request=false;
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}
else{
try{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例。");
return false;
}
if(responseType.toLowerCase()=="text"){
http_request.onreadystatechange=callback;
}else{
window.alert("响应类别参数错误。");
return false;
}
if(method.toLowerCase()=="get"){
http_request.open(method,url,true);
http_request.setRequestHeader("If-Modified-Since", "0"); //清除缓存啊
}
else if(method.toLowerCase()=="post"){
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}else{
window.alert("http请求类别参数错误。");
return false;
}
http_request.send(content);
}
//处理返回文本格式信息的函数
function processTextResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("Text文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function processXMLResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("XML文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function callback(){
if (http_request.readyState == 1)
{
document.getElementById("mydiv").innerHTML = "正在加载连接对象......";
}
if (http_request.readyState == 2)
{
document.getElementById("mydiv").innerHTML = "连接对象加载完毕。";
}
if (http_request.readyState == 3)
{
document.getElementById("mydiv").innerHTML = "数据获取中......";
}
if(http_request.readyState==4)
{
if(http_request.status==200)
{
showmsg();//处理服务器返回的数据
}
}
}
//查询的请求
function userinforequest(){
var url='Handler2.ashx';
send_request("GET",url,null,"text",callback);
}
</script>
然后我在页面的body里调用userinforequest()
<body onload="userinforequest()">
<div id="mydiv"></div>
</body>结果:数据获取中......一直都是这样了就,问题出在哪呢?
<script language='javascript'>
var http_request = false;
function send_request(method,url,content,responseType,callback){
http_request=false;
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}
else{
try{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例。");
return false;
}
if(responseType.toLowerCase()=="text"){
http_request.onreadystatechange=callback;
}else{
window.alert("响应类别参数错误。");
return false;
}
if(method.toLowerCase()=="get"){
http_request.open(method,url,true);
http_request.setRequestHeader("If-Modified-Since", "0"); //清除缓存啊
}
else if(method.toLowerCase()=="post"){
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}else{
window.alert("http请求类别参数错误。");
return false;
}
http_request.send(content);
}
//处理返回文本格式信息的函数
function processTextResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("Text文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function processXMLResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("XML文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function callback(){
if (http_request.readyState == 1)
{
document.getElementById("mydiv").innerHTML = "正在加载连接对象......";
}
if (http_request.readyState == 2)
{
document.getElementById("mydiv").innerHTML = "连接对象加载完毕。";
}
if (http_request.readyState == 3)
{
document.getElementById("mydiv").innerHTML = "数据获取中......";
}
if(http_request.readyState==4)
{
if(http_request.status==200)
{
showmsg();//处理服务器返回的数据
}
}
}
//查询的请求
function userinforequest(){
var url='Handler2.ashx';
send_request("GET",url,null,"text",callback);
}
</script>
然后我在页面的body里调用userinforequest()
<body onload="userinforequest()">
<div id="mydiv"></div>
</body>结果:数据获取中......一直都是这样了就,问题出在哪呢?
{
if(http_request.status==200)
{
showmsg();//处理服务器返回的数据
}
}
你在完成的时候什么都没做,那不就一直显示"数据获取中......"
了?
http_request.open(method,url,true);
http_request.setRequestHeader("If-Modified-Since", "0"); //清除缓存啊
}
else if(method.toLowerCase()=="post"){
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}else{
window.alert("http请求类别参数错误。");
return false;
}移至 if(!http_request){这个以前.
根本就没有走到这条语句来,停留在
if (http_request.readyState == 3)
{
document.getElementById("mydiv").innerHTML = "数据获取中......";
}
现在的问题是它根本就走不到这个地方来,所以看到与看不到showmsg()没有关系的ps:试过你给的方法,还是不行呢
{
if(http_request.status==200)
{
document.getElementById("mydiv").innerHTML = "数据获取完毕";
}
}
你这么改看看到底什么效果????
还有你用中断调试逐步运行看了保证没走到这步????如果没走到这步那就是你的Handler2.ashx这个有问题
{
if(http_request.status==200)
{
showmsg();//处理服务器返回的数据
}
} 走到if(http_request.readyState==4) 然后直接跳出,里面的没有执行了
<NewDataSet>
- <Table>
<id>27</id>
<userName>BECKHAM</userName>
<userPwd>小贝</userPwd>
</Table>
- <Table>
<id>28</id>
<userName>肥罗</userName>
<userPwd>外星人</userPwd>
</Table>
</NewDataSet>这个有什么问题么?是不是还要加头文件啊?
我按你的代码试过了,没有问题,Handler2.ashx这个我自己写
这一步要执行到需要从Handler2.ashx获得返回值了才可以执行,检查一下这个文件是否有输出吧
{
if(http_request.status==200)
{
showmsg();//处理服务器返回的数据
}
} 我加了个判断,确定http_request.status==404,这个问题的原因是?
using System.Web;
using System.Data.OleDb;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
try
{
context.Response.Write("123");
}
catch
{ }
}
public bool IsReusable {
get {
return false;
}
}}这个读取没问题
程序本身没有错误,错误是因为VS2005不支持中文路径