< script >
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && (xmlhttp.status>=200&&xmlhttp.status<=300))
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}else{
alert(xmlhttp.status);
}
}
xmlhttp.open("GET","https://api.shenjian.io/promovie/piaofang?appid=fed023bfdffa5202e08665601d127045/",true);
xmlhttp.send(null);
}
< / script >
write this for the first time, ask the boss to see what the problem is?
ajax, encapsulated with jQ can successfully obtain data by changing datatype from JOSN to JSONP, but if you want to implement it with a native method, it"s still a big problem
< script >
var url="https://api.shenjian.io/promovie/piaofang?appid=fed023bfdffa5202e08665601d127045";
//xhr
function createCORSXhr(url,method){
var xhr=new XMLHttpRequest();
if("withCredentials" in xhr){
xhr.open(method,url,true);
}else if(typeof XDomainRequest !=="undefined"){
xhr=new XDomainRequest();
xhr.open(method,url);
}else
xhr=null;
return xhr;
}
//ajax
function sendAjaxRequest(){
var xhr=createCORSXhr(url,"get");
//xhr.setRequestHeader("Origin","https://api.shenjian.io/promovie/piaofang?appid=fed023bfdffa5202e08665601d127045")
xhr.onload=function(){
if(xhr.readyState==4){
if(xhr.status>=200 && xhr.status<300 || xhr.status==304)
alert(xhr.responseText);
}else{
alert(" ajax error...")
}
}
};
xhr.onerror=function(){
alert("error code:"+xhr.status)
}
xhr.send(null);
};
sendAjaxRequest()
< / script >
someone wrote this on the Internet. He said that he could support cross-domain, but I tried to report the same mistake