Ajax关于重定向

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;
        }

  防止页面嵌套。

时间: 2024-08-22 01:47:20

Ajax关于重定向的相关文章

学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向

数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView("index", map);, * 相当于把结果数据放到request里面 * @return * @throws Exception */ @RequestMapping("/toPerson4.do") public ModelAndView toPerson4() throws Exception{ Per

springmvc返回值、数据写到页面、表单提交、ajax、重定向

实验是在前一篇文章的项目上做的: 数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView("index", map);, * 相当于把结果数据放到request里面 * @return * @throws Exception */ @RequestMapping("/toPerson4.do") public ModelAndView toPerson4() thro

ajax请求后台,response.sendRedirect失效,无法重定向

今天在写项目的时候,想加一个切换用户,需要清除session并且跳转到登录页面,发起一个ajax请求后,执行完发现无法跳转. 原因在于: (从网上摘录) Ajax只是利用脚本访问对应url获取数据而已,不能做除了获取返回数据以外的其它动作了.所以浏览器端是不会发起重定向的. 1)正常的http url请求,只有浏览器和服务器两个参与者.浏览器端发起一个http请求,服务器端处理后发起一个重定向,浏览器端从response中获取到重定向地址,发起另一个http url请求.也就是说,浏览器会按照r

ajax中向页面中指定位置添加信息

$.ajax({  type : "POST",  beforeSend : function() {   showLoader("数据加载中...");// 展示等待效果  },  complete : function() {   //与后台通讯(查询是否存在有效保单)    getComPlugin(successCallback,"com.sinosoft.hna.lpcx.LPSQJYSrarch",'searchSQJY',dates

ajax异步请求302

我们知道,只有请求成功ajax才会进行回调处理,具体状态码为 status >= 200 && status < 300 || status === 304; 这一点通过查看JQuery的源码就可以证实. // Cache response headers responseHeadersString = headers || ""; // Set readyState jqXHR.readyState = status > 0 ? 4 : 0; //

servlet的四种响应

在一个servlet的请求中,响应的方式的通常有四式,response.getWriter(),response.getOutputStream(), request.getRequestDispatcher("ajax.jsp").forward(request, response) 和 response.sendRedirect("ajax.jsp") . 其中 response.getWriter() 输出页面 response.getOutputStream

Django框架基础(二)

MODELS 1 对数据库起效的字段设置: 2 数据类型:CharField 3 IntegerField 4 FloatField 5 DecimalField(max_digits=30,decimal_places=10) 6 DateTimeField 7 8 9 字段参数:null=True default='' unique=True db_index=True premary_key 10 class Meta: 11 unique_together=( 12 ('email','

flask模板template 与js 的一些问题

众所周知,flask使用的模板语言是jinja2,关于jinja的快速入门可以看 https://spacewander.github.io/explore-flask-zh/8-templates.html. 我在模板中使用{{}}来获取变量没有问题,在模板页面里使用JavaScript函数也没有问题.有几个需要注意的点: js 函数中,ajax请求重定向. function sale_submit(obj){ var words='a'; var page=2; window.locatio

ajax请求不能重定向问题探究及解决【借鉴、参考】

[声明]本文章是为了解决个人项目中遇到的ajax请求与重定向的问题,参考了多位网友的博客和意见,感谢这些网友! 1.原因探究及解决方法(参考网友,只做以后查询用) response.sendRedirect做转向的原理,它其实是向浏览器发送一个特殊的Header,然后由浏览器来做转向,转到指定的页面,所以用sendRedirect时,浏览器的地址栏上可以看到地址的变化. 而ajax请求是局部刷新,默认是不支持重定向或转发,因此需要对请求进行判断: 权限拦截器代码示例: 页面要设置全局js,重写a