jQuery.ajax()
注意事项
$.ajax({
method: 'POST',
url: '/register',
contentType: 'text/json; charset=UTF-8',
data: {
name,
password
}
}).then(() => {
alert('注册成功')
location.href = '/sign_in.html'
}, () => {})
如果data是 对象类型,那么请求时候将会按照name=xxx&password=xxx 这样传输
AXIOS
可以操作
使用Promise封装XMLHttpRequest
function ajax(method, url) {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open(method, url);
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status === 200) {
resolve(request.response);
} else {
reject(request);
}
}
};
request.send();
});
}
ajax("get", "http://qq.com:8888/friends.json").then(response => {
console.log("这是 AJAX");
console.log(response);
});