当前位置: 移动技术网 > IT编程>开发语言>Java > Javaweb使用cors完成跨域ajax数据交互

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对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> 

<!-- 跨域问题 --> 
  <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节点,如果允许所有站点跨域访问,可以修改为[*],如果是多个站点,可以用[,]分隔配置。

<!-- 跨域问题 --> 
  <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跨域调用数据,实例代码如下:

<!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数据交互的全部内容,希望对大家有所帮助。如果在阅读过程中有什么问题,可以给小编留言,我会及时回复大家的。也希望大家多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网