1:用第三方接口容易被跨越拦截
<body>
<button id="getMusic">GET</button>
<button id="post">POST</button>
<script>
var url = 'http://musicapi.leanapp.cn/artist/list';
//上面是第三方接口
var params ={
page:1,
a:2
}
// get 请求得 请求参数通过URL 得知传递
getMusic.onclick=function(){
var xhr =new XMLHttpRequest();
url = url+"?"
for(var x in params){
url =url+x+'='+params[x]+"&"
}
url =url.slice(0,length-1)
xhr.open('GET',url,true);
xhr.send();
xhr.onreadystatechange =function(){
if(xhr.readyState==4&& xhr.status ==200){
console.log(xhr.responseText);
}
}
}
post.onclick =function(){
var xhr =new XMLHttpRequest();
xhr.open('POST',url,true);
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
xhr.send('page=1&a=2');
xhr.onreadystatechange =function(){
if(xhr.readyState == 4&& xhr.status==200){
console.log(xhr.responseText);
}
}
}
</script>
</body>