进一步查看异步请求

本文深入探讨了浏览器的同源策略,解释了为何它限制页面与不同源交互。同时,通过对比AJAX与普通HTTP请求,强调了AJAX请求的特性和在跨域时面临的挑战,包括OPTIONS请求预检以及请求头中的x-requested-with参数。此外,还指出AJAX无法在服务器端执行重定向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同源策略

URL由协议,域名,端口和路径组成,如果两个URL的协议,域名和端口相同,
则表示他们同源。相反,只要协议,域名,端口有任何一个的不同,就被当做是跨域。

浏览器采用同源策略,禁止页面加载或执行与自身来源不同的域的任何脚本

但是部分请求不受到同源策略的限制,比如:

<script><img><iframe><link>
这些包含src属性的标签可以加载跨域资源

观察同源策略

$(function(){
	//为超链接绑定事件,点击之后会发送请求
	var emp;
	$("a").click(function(){
		//发送请求
		$.ajax({
			type:"post",
			url:"https://gd2.alicdn.com/imgextra/i2/3154470202/O1CN01mGfaQ81DMYVPwXSXS_!!3154470202.jpg",
			data:"id=7788",
			dataType:"json",
			async:false,//锁定浏览器,只有请求处理完毕之后(回调函数调用完毕之后)才能执行后面的代码
			success:function(data){
				emp=data;
			}
		})
		alert(emp.ename);
	})
})

在这里插入图片描述
同源策略是浏览器提供的一种安全机制

AJAX和普通HTTP请求的区别

从本质上讲:AJAX就是浏览器发出的HTTP请求

AJAX和HTTP的区别:

  • AJAX就是浏览器使用XMLHTTPRequest对象发出的HTTP请求,但是,是;浏览器加上了一个同源策略限制而已,从本质上讲,AJAX就是浏览器发出的HTTP请求
  • AJAX请求受到浏览器的同源策略,存在跨域问题
  • AJAX在发送复杂请求时,浏览器会预先发出OPTIONS请求预检(HTTP是不会预检的)
  • AJAX请求头会多一个x-requested-with参数,值为XMLHttpRequest
  • AJAX请求不能在控制器中进行跳转(转发、重定向)

观察请求头信息

@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//取得所有请求头信息
		Enumeration<String> headers = req.getHeaderNames();
		while (headers.hasMoreElements()) {
			String header = headers.nextElement();
			System.out.println(header+"="+req.getHeaderNames());
		}
	}
$(function(){
	//为超链接绑定事件,点击之后会发送请求
	var emp;
	$("a").click(function(){
		//发送请求
		$.ajax({
			type:"get",
			url:"emp/get",
			data:"id=7788",
			dataType:"json",
			async:false,//锁定浏览器,只有请求处理完毕之后(回调函数调用完毕之后)才能执行后面的代码
			success:function(data){
				//emp=data;
			}
		})
		//alert(emp.ename);
	})
})

异步请求:有x-requested-with

host=org.apache.tomcat.util.http.NamesEnumerator@65bf5fee
connection=org.apache.tomcat.util.http.NamesEnumerator@6c50042
accept=org.apache.tomcat.util.http.NamesEnumerator@48408958
x-requested-with=org.apache.tomcat.util.http.NamesEnumerator@7eaea6fc
user-agent=org.apache.tomcat.util.http.NamesEnumerator@61f1c6e8
referer=org.apache.tomcat.util.http.NamesEnumerator@4f181d84
accept-encoding=org.apache.tomcat.util.http.NamesEnumerator@35e5776
accept-language=org.apache.tomcat.util.http.NamesEnumerator@8cc88a7
cookie=org.apache.tomcat.util.http.NamesEnumerator@67e5c9d0

普通请求:
没有x-requested-with

host=org.apache.tomcat.util.http.NamesEnumerator@72fc41c6
connection=org.apache.tomcat.util.http.NamesEnumerator@23b22e6d
upgrade-insecure-requests=org.apache.tomcat.util.http.NamesEnumerator@3853cd27
user-agent=org.apache.tomcat.util.http.NamesEnumerator@79d912ad
accept=org.apache.tomcat.util.http.NamesEnumerator@69bb9e21
accept-encoding=org.apache.tomcat.util.http.NamesEnumerator@5b48043d
accept-language=org.apache.tomcat.util.http.NamesEnumerator@7dd4d086
cookie=org.apache.tomcat.util.http.NamesEnumerator@4c29e9eb

AJAX不能在控制器中跳转

OPTIONS请求预检

post请求和get请求算是简单请求,除了这两种之外的都认为是复杂请求(比如:put,delete,trace等)都是复杂请求,当使用AJAX发送复杂请求的时候会先发送OPTIONS请求进行预检,发送简单请求浏览器不会发送预检请求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值