通用各大浏览器跨域web配置

在将下面这段放到web.xml上
<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>*</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>  

这些GET, POST, HEAD, PUT, DELETE 是参数请求方式,设置了这些避免跨域困扰

使用jar包括 cors-filter-1.7.jar 和gson-2.6.2.jar,java-property-utils-1.9.1.jar三个。

前端模拟请求


<script type="text/javascript">
$(function() {
$("#btn").on('click',function() {
var callback = $('#callback').val(),
userId = $('#userId').val(),
productId = $('#productId').val(),
score = $('#score').val(),
content = $('#content').val(),
mac = $('#mac').val(),
obj = {callback:callback,userId:userId,productId:productId,score:score,content:content};// 定义参数
var url = 'http://192.168.1.112:8080/hnstore/api/addProductScoreBS.action';//定义请求地址
$.ajax({
                    type: "post",//请求方式
url:url,
data :{"datalist":JSON.stringify(obj)},//放在方法里的
                    beforeSend: function(request) {//设置header的参数
                         request.setRequestHeader("mac",mac);
},
success: function (data) {//成功返回
$('#result2').html( url);
$('#result5').html("post");
$('#result3').html(JSON.stringify(obj));
$("#result").html(data);
},
error: function(data) { //失败返回
alert("操作失败");
}
                 });
});


$('#btn2').on('click',function() { 
$('#callback').val("");
$('#productId').val("");
$('#userId').val(""); 
$('#score').val("");
$('#content').val("");
$('#mac').val("");
$('#result2').html("");
$('#result3').html("");
$('#result').html("");
});




var opt = {
"callback ": ({
"returnCode": "0",
"result": "评分成功"
}) 
};
$('#showResult').JSONView(opt);
});






</script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值