Javaweb使用cors完成跨域ajax数据交互

2025-05-29 0 32

跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制。

ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作,所以会警告。

cors

全称:Cross-Origin Resource Sharing

中文意思:跨域资源共享

它在维基百科上的定义是:跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源。而这种访问是被同源策略所禁止的。CORS系统定义了一种浏览器和服务器交互的方式来确定是否允许跨域请求。 它是一个妥协,有更大的灵活性,但比起简单地允许所有这些的要求来说更加安全。

1、通过Maven引用

cors-filter、<span style="font-family: Arial, Helvetica, sans-serif;">java-property-utils二个jar包,修改pom.xml文件,加入下面内容</span>

?

1

2

3

4

5

6

7

8

9

10

11
<!-- 跨域问题 -->

<dependency>

<groupId>com.thetransactioncompany</groupId>

<artifactId>cors-filter</artifactId>

<version>2.5</version>

</dependency>

<dependency>

<groupId>com.thetransactioncompany</groupId>

<artifactId>java-property-utils</artifactId>

<version>1.10</version>

</dependency>

2、在web.xml里面配置过滤器,使用引入的jar中定义好的过滤器。注意修改cors.allowOrigin节点,如果允许所有站点跨域访问,可以修改为[*],如果是多个站点,可以用[,]分隔配置。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30
<!-- 跨域问题 -->

<filter>

<description>跨域过滤器</description>

<filter-name>CORS</filter-name>

<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

<init-param>

<param-name>cors.allowOrigin</param-name>

<param-value>https://127.0.0.1:8380</param-value>

</init-param>

<init-param>

<param-name>cors.supportedMethods</param-name>

<param-value>GET, POST, HEAD, PUT, DELETE</param-value>

</init-param>

<init-param>

<param-name>cors.supportedHeaders</param-name>

<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>

</init-param>

<init-param>

<param-name>cors.exposedHeaders</param-name>

<param-value>Set-Cookie</param-value>

</init-param>

<init-param>

<param-name>cors.supportsCredentials</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CORS</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

3、通过jQuery跨域调用数据,实例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55
<!DOCTYPE html>

<html lang="en" xmlns="https://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />

<title>跨域测试</title>

<style type="text/css">

body{

margin:0px auto 0px auto;

}

.p_container {

margin: 0px auto 0px auto;

width: 100%;

height: 200px;

}

.p_container > iframe {

width: 100%;

height: 100%;

}

</style>

</head>

<body>

<p>

</p>

<button id="btn_test">跨域调用</button>

<p id="p_show"></p>

<script src="jquery-1.8.3.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function () {

$('#btn_test').click(function () {

//alert('dddd');

//var iframe_main = $("#iframe_main").contents();

//iframe_main.find("#account").val('test');

$.ajax({

url: "https://10.18.25.119:8480/jxfp/index.jsp",

type: "GET",

dataType: "text",

timeout: 10000,

xhr: function () { //这是关键 获取原生的xhr对象 做以前做的所有事情

var xhr = jQuery.ajaxSettings.xhr();

xhr.withCredentials = true;

return xhr;

},

success: function (data) {

$("#p_show").html(data);

//Console.log(data);

},

error: function (e) {

$("#p_show").html(e.statusText);

}

});

});

});

</script>

</body>

</html>

以上就是是小编分享给大家的Javaweb使用cors完成跨域ajax数据交互的全部内容,希望对大家有所帮助。如果在阅读过程中有什么问题,可以给小编留言,我会及时回复大家的。也希望大家多多支持快网idc!

原文链接:http://blog.csdn.net/nnsword/article/details/53905592

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Javaweb使用cors完成跨域ajax数据交互 https://www.kuaiidc.com/114826.html

相关文章

发表评论
暂无评论