簡單點直接后端寫類似res.header("Access-Control-Allow-Origin", "*");
來同意任何域的訪問
或者
res.header("Access-Control-Allow-Origin", "a.com");
特定指定一個a.com這種域名的跨域訪問
要么用JSONP吧
function $(id){
if(document.querySelectorAll(id).length > 1){
return document.querySelectorAll(id);
}else{
return document.querySelector(id);
}
}
var txt = $("#txt"),
ul = $("#baidusug"),
script = null;
txt.onkeyup = function (){
ul.innerHTML = "";
if (script) {
document.body.removeChild(script);
}
script = document.createElement("script");
script.src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" + txt.value + "&cb=process";
document.body.appendChild(script);
};
function process(json){
for(var i = 0; i < json["s"].length; i++){
var li = document.createElement("li");
li.innerHTML = json.s[i];
ul.appendChild(li);
}
}
比如如上代碼豁鲤,粘貼IDE理解含義。
terminal: vi /etc/hosts 設(shè)置比如
127:0:0:1 a.com
127:0:0:1 b.com
這么模擬跨域操作