首页 > Web开发 > 详细

Ajax关于重定向

时间:2014-11-17 17:36:33      阅读:326      评论:0      收藏:0      [点我收藏+]

AJAX即Asynchronous Javascript And Xml,异步javascript和xml,主要用户在不刷新页面的情况下与服务器数据交互。

Ajax主要用到的对象为XMLHttpRequest(在IE5、IE6中为ActiveXObject)

if(window.XMLHttpRequest){

  xmlhttp = new XMLHttpRequest();

}

else{

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

}

xmlhttp.open(method,url,async);

xmlhttp.setRequestHeader()

  xmlhttp.onreadystatechange = function(){

    if(xmlhttp.readyState ==4&&xmlhttp.status==200){

      xmlhttp.getResponseHeader(string)

      xmlhttp.responseXML

      xmlhttp.responseText

    }

  }

  xmlhttp.send(string);

---------------------------------------------------------------------------------------------------------

  &.ajax({

    type:"GET",

    url:"http://",

    success:function(msg){},

    error:function(xmlhttprequest,textstatus,errorThrow){},

    statusCode:{

      404:function(){}

    },

    complete:function(xmlhttprequest,textstatus){}

  });

  正常情况下,ajax向指定url以指定方法发送数据,服务器处理完之后返回状态200,且content-type:text/plain的数据,客户端接收到返回的数据进行处理。如果发送ajax数据时session已过期,服务器要求重定向到登陆页面,此时前台并不能获取到302的状态码,XMLHttpRequest对象会直接向后台发起重定向请求,然后返回状态200,content-type:text/html;msg以及responseText为登陆页面的html。

  在complete中判断如果返回的content-type为text/html则显示该页面

  document.write(xmlhttprequest.responseText);

  document.close();(必须的)

  如果ajax请求是在嵌套页面中发送的,登陆页面要显示在最顶层的页面中,可以在登录页面中调用

  if (top.location != window.location) {
            top.location = window.location;
        }

  防止页面嵌套。

Ajax关于重定向

原文:http://www.cnblogs.com/chunchengwuchu/p/4103781.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!