实现跨域有以下几种方案:
- 服务器端运行跨域 设置 CORS 等于 *;
- 在单个接口使用注解 @CrossOrigin 运行跨域;
- 使用 jsonp 跨域;
1.ajax中添加“xhrFields”和“crossDomain”,如:
$.ajax({
url: url,
data: data,
type: "post",
xhrFields: {
withCredentials: true
},
crossDomain: true,
async: false,
success: function (result) {
}
});
2.xhrFields是一个具有多个"字段名称-字段值"对的对象,用于对本地XHR对象进行设置。一对「文件名-文件值」在本机设置XHR对象。