攻城狮-web

JSON数据加载

json数据加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
案例分析
var box = document.querySelector('div')
var btn = document.querySelector('button')
btn.onclick = function(){
var xhr = new XMLHttpRequest();
//输入网址,参数一是请求方式
xhr.open('get','true')
//发起请求
xhr.send();
xhr.onreadyStateChange = function(){
if(xhr.readyState===4){
if(xhr.status===200){
console.log(xhr.responseText)
function json_callback(data){
console.log(data)
}
//利用javaScript的src不存在跨域问题,相当于函数参数调用,参数是我们需要的数据
json_callback({
name:'jsonData'
})
}
}
}
}

跨域:不同域相互请求资源