Home > Backend Development > PHP Tutorial > javascript - 用ES6的fetch提交的数据,PHP要如何获取呢

javascript - 用ES6的fetch提交的数据,PHP要如何获取呢

WBOY
Release: 2016-06-06 20:06:59
Original
1675 people have browsed it

前台的代码:

<code>const req = {
    name: 'Wing',
    age: 32,
    skills: ['HTML', 'CSS', 'JavaScript']
  };
  fetch('/actions/user/info', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(req)
  }).then(response => response.json())
    .then(json => {
      console.log(json);
    });</code>
Copy after login
Copy after login

后端用的PHP,现在我用file_get_contents("php://input");能获取到传过来的数据,可是拿到的是原始的一个JS 对象,要如何转换成PHP数组呢?

或者,还能有其他办法来获取数据么?

回复内容:

前台的代码:

<code>const req = {
    name: 'Wing',
    age: 32,
    skills: ['HTML', 'CSS', 'JavaScript']
  };
  fetch('/actions/user/info', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(req)
  }).then(response => response.json())
    .then(json => {
      console.log(json);
    });</code>
Copy after login
Copy after login

后端用的PHP,现在我用file_get_contents("php://input");能获取到传过来的数据,可是拿到的是原始的一个JS 对象,要如何转换成PHP数组呢?

或者,还能有其他办法来获取数据么?

这样过去的应该是JSON格式:
http://www.ruanyifeng.com/blog/2011/01/json_in_php.html

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template