ajax的post请求如何让web或接口的方法支持跨域()

只是方法,如果是服务器或者整个web,找其他资料
只需要加入
response.setHeader("Access-Control-Allow-Origin", "*");
即可
 
  

@ResponseBody
	@RequestMapping(value="login1", method=RequestMethod.GET, produces="application/json;charset=UTF-8")
	public String login1(HttpServletRequest request,HttpServletResponse response) throws Exception{
		response.setHeader("Access-Control-Allow-Origin", "*"); 
@ResponseBody
	@RequestMapping(value="login", method=RequestMethod.POST, produces="application/json;charset=UTF-8")
	public String login(@RequestBody String requestJson,HttpServletRequest request,HttpServletResponse response) throws Exception{
		//设置跨域请求
		response.setHeader("Access-Control-Allow-Origin", "*");

通过浏览器请求get

获取通过360浏览器的console请求post

var data={
				    thirdType:3,
				    openId:"a970a9778",
				    userName:"22288",
				    headImage:"http://www.baidu.com/image.png"
				}
	        	$.ajax({
	    			type:"POST",
	    			dataType:"json",
	    			url:"http://192.168.1.5:8080/ryqp/login.shtml",
	    			data:data,
	    			success: function(msg){
	    				if(msg.code == "APP.000"){
	    					alert('删除成功');
	    				}else  {
	    					alert("删除失败!");
	    				}
	    			},             
	    	        error: function (msg) {                    	
	    				alert("删除失败!");
	    	        } 
	    		});


你可能感兴趣的:(跨域,ajax)