1、在js中使用CROS来进行跨域提交
function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { // "withCredentials"属性是XMLHTTPRequest2中独有的 xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { // 检测是否XDomainRequest可用 xhr = new XDomainRequest(); xhr.open(method, url); } else { // 看起来CORS根本不被支持 xhr = null; } return xhr; }; function crossDomainRequest(url, fun) { var xhr = createCORSRequest('GET', url); if (!xhr) { alert('CORS not supported'); return; } // 回应处理 xhr.onload = function() { fun(xhr); }; xhr.onerror = function(e) { alert('Woops, there was an error making the request.'+e); }; xhr.send(); };
2、在服务端需要设置允许CROS
resp.setHeader("Access-Control-Allow-Origin", "*");