java jq 跨域问题、Json数据传递 【Access-Control-Allow-Origin】

来源:http://yuxisanren.iteye.com/blog/2019666

Java服务器端

/*
*java
*coder:宝贤
*/
 String callback = request.getParameter("jsonpCallback");  
		String result = callback + "(" +output+ ")";  
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		response.addHeader("Access-Control-Allow-Origin","*");//'*'表示允许所有域名访问,可以设置为指定域名访问,多个域名中间用','隔开
		
		if("IE".equals(request.getParameter("type"))){
	           response.addHeader("XDomainRequestAllowed","1");
	       }
	       out.print("success");
	       
		out.print(result);
		out.close();

前端Js

/*
*js
*coder:janing
*/

url="http://xxxxxx/Pay2/View2.java";
_post="";
$.ajax({
  "url" : url,
  "type":"post",
  "data":_post,
  "success":function(ret){
    console.log($.parseJSON(ret));
  }
})