jQuery.ajax()

注意事项

  1. $.ajax({
  2. method: 'POST',
  3. url: '/register',
  4. contentType: 'text/json; charset=UTF-8',
  5. data: {
  6. name,
  7. password
  8. }
  9. }).then(() => {
  10. alert('注册成功')
  11. location.href = '/sign_in.html'
  12. }, () => {})

如果data是 对象类型,那么请求时候将会按照name=xxx&password=xxx 这样传输

AXIOS

可以操作

使用Promise封装XMLHttpRequest

  1. function ajax(method, url) {
  2. return new Promise((resolve, reject) => {
  3. const request = new XMLHttpRequest();
  4. request.open(method, url);
  5. request.onreadystatechange = () => {
  6. if (request.readyState === 4) {
  7. if (request.status === 200) {
  8. resolve(request.response);
  9. } else {
  10. reject(request);
  11. }
  12. }
  13. };
  14. request.send();
  15. });
  16. }
  17. ajax("get", "http://qq.com:8888/friends.json").then(response => {
  18. console.log("这是 AJAX");
  19. console.log(response);
  20. });