LoginSignup
2

More than 5 years have passed since last update.

Fetch APIでPOSTしたら、$_POSTで取得できなかった

Last updated at Posted at 2017-09-06

Content-Typeをちゃんと指定しないとダメだった。

バージョン

php

# php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Firefox

55.0.3
https://www.mozilla.org/en-US/firefox/55.0.3/releasenotes/

ダメだったやつ

fetch(
    url,
    {
        method: 'POST',
        mode: 'cors',
        body: 'key1=value1&key2=value2'
    }
).then(function(response) {
    console.log(response);
}).catch(function(e) {
    console.error(e);
});

OKだったやつ

fetch(
    url,
    {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        mode: 'cors',
        body: 'key1=value1&key2=value2'
    }
).then(function(response) {
    console.log(response);
}).catch(function(e) {
    console.error(e);
});

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
2