前后台分离开发 后台进行跨域设置

    	<!-- 跨域请求设置开始 -->
	<filter>
		<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>
	<!-- 跨域请求设置结束 -->

需要在web.xml中添加上面这个filter

下面是我测试用的前台页面的一些参数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>bitch</title>
     <script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
     <script type="text/javascript">
    jQuery(document).ready(function(){
        $.ajax({
            type : "get",
            async:false,
            url: 'http://localhost:8080/share_god/user/login.action',
            dataType : "jsonp",
            jsonp: "callbackparam",
			data: {
			"tel":'18838181888',
			"code":'1111'
			},
			xhrFields: {
            withCredentials: true
       },
			crossDomain: true,
            jsonpCallback:"success_jsonpCallback",
            success : function(json){
                alert(json.json_code);
            },
            error:function(){
                alert('fail');
            }
        });
    });
    </script>
	
    </head>
 <body>
 </body>
</html>


下面我放一个我测试用的action

@RequestMapping(value = "/test.action")
@ResponseBody
public String getMobileAuthCode( HttpServletRequest request,HttpServletResponse response, String callbackparam)
        throws Exception {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

System.out.println(request.getSession().getId());
String result =  "{'ret':'true'}";
    //加上返回参数
    result=callbackparam+"("+result+")";
   /*response.getWriter().write(result);*/
    return result;
}

备注;共享珠宝项目

猜你喜欢

转载自blog.csdn.net/zhangzuyuanbest/article/details/78419998