Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

AjaxでJSON取得

req.openの2つ目は非同期のフラグ、req.sendはPOSTの場合データが入る。

      const req = new XMLHttpRequest();
      req.open("GET", "myjson.json", true);
      req.onreadystatechange = () => {
        if (req.readyState === 4 && req.status === 200) {
          const jsonText = req.responseText;
          const json = JSON.parse(jsonText);
          this.resultAjax = jsonText;
        }
      }
      req.send(null);