package ll.highway.util; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @author zhangcunli * 解決跨域問題 */ @Configuration public class Cors extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH") .allowCredentials(true).maxAge(3600); } }
須要跨域的Controller繼承這個類,就能夠實現跨域了。前端
前端實現ios
//全局變量 Vue.prototype.baseUrl = "http://IP地址:81/"; //封裝axios const instance = axios.create({ baseURL: Vue.prototype.baseUrl, timeout: 10000, withCredentials: true,//跨域 headers: { "Content-Type": "application/x-www-form-urlencoded" }//跨域 }); Vue.prototype.axios = instance;
var rh = new Object(); rh.ReqId = "ls123"; rh.Salt = "sssseee"; var rb = new Object(); rb.CatId = "00001"; this.axios({ method: "post", url: "zdm/AppApi/Search/CategoryList", data: { ReqHead: rh, ReqBody: rb } }) .then(function(response) { _this.msg = response; }) .catch(function(error) { console.log(error); });