0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Fetch API] Ajaxの代わりのありがちなアレ

Last updated at Posted at 2018-12-07

最近Fetch APIに興味を持ち始めて、ぼちぼち調べている。
Promiseって、理解すると凄く便利そうだなぁと思う今日この頃。

とりあえず、Ajaxでうんたらかんたらする代わりに、fetchでやってみよう!というお話。
とにかく、シンプルで使い回せそうなものを見付けるのである (´・ω・`)

hoge.js
function hoge() {
    var form_data = new FormData();
    form_data.xxx_id = 1;

    fetch('./hoge.php', {
       method: 'POST',
       body: form_data 
    })
    .then(response => {
        return response.json();
    })
    .then(json=> {
        console.log(json.data);
    });
}

fetch()が済んだらresponseを返し、そこからjsonを取り出して値を取り出して。
いぇー ( ・`ω・´)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?