spring框架解决跨域问题
1.为什么会发生产生跨域问题?
因为浏览器为了安全(同源),本身就限制了。
当我们发送XMLHttpRequest请求的时候,如果请求的是别的域(主机域名、端口)不同时,那么就会产生跨域问题(客户端无法获取服务端返回的数据)
如果不是XMLHttpRequest请求
2.解决方案
在对应的Controller中使用注解@CrossOrigin
package ths.project.exhibition.web.dp.pollute;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ths.jdp.core.web.base.BaseController;
import ths.project.exhibition.service.dp.PollutService;
/**
* @param desigerid
* @return
*/
@Controller
@CrossOrigin // 支持跨域
@RequestMapping("/dp/pollution")
public class PollutController extends BaseController {
@Autowired
private PollutService pollutService;
/**
*
* @method: index
*/
@RequestMapping("/index")
public ModelAndView index() {
ModelAndView result = new ModelAndView("dp/pollution/pollution");
return result;
}
/**
*
* @method: getList
*/
@RequestMapping("/getList")
@ResponseBody
public List<Map<String,Object>> getList(@RequestParam Map<String,Object> param) {
return pollutService.getList(param);
}
}
@CrossOrigin该注解可以使用在类上和方法上。