LoginSignup
7
11

More than 5 years have passed since last update.

バニラJSでajaxを実装してみる

Posted at

個人的なメモとして残しておく

なぜVaillaJSを使うのか

なぜVanilla JSを使うのかというととにかくパフォーマンスがいいそうで。
下のサイトに書かれています。
http://vanilla-js.com/

爆速で有名のdev.toもVanllaだそうです。

Ajaxを実現してみる

  • jQueryの場合

$.ajax({
  type: 'GET',
  url: '/my/url',
  success: function(resp) {

  },
  error: function() {

  }
});

  • VanillaJSの場合

      var request = new XMLHttpRequest();
      request.open('GET', '/my/url', true);

      request.onload = function() {
          if (request.status >= 200 && request.status < 400) {
              // Success!
              console.log("成功")
          } else {
              console.log("失敗")
          }
      };

      request.onerror = function() {
          console.log("失敗")
      };

      request.send();

なにか間違ってたりしたらコメントください。お願いします😓

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11