0%

Js筆記 AJAX

AJAX

XMLHttpRequest

open 格式

onlaod

確定資料回傳,執行以下函式

1
2
3
4
5
6
7
8
9
function getData() {
let xhr=new XMLHttpRequest();
xhr.open('get',url,true);
xhr.send(null);
console.log(xhr.responseText)
xhr.onload=function () {
let data1 = JSON.parse(xhr.responseText)
return data=data1.ROOT.RECORD
}}

asios

1
2
3
4
5
6
7
// axios
function getData(){
axios.get(url)
.then(function (response) {
return data=response.data.ROOT.RECORD
});
}

jQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
function getData() {
$(document).ready(function () {
$.ajax({
type: "get",
url: url,
data: "data",
dataType: "json",
success: function (response) {
return data=response.ROOT.RECORD
}
});
});
}

範例