來源:互聯網
上載者:User
關鍵字
javascript
fetch
php
es2015
前台的代碼:
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); });
後端用的PHP
,現在我用file_get_contents("php://input");
能擷取到傳過來的資料,可是拿到的是原始的一個JS 對象
,要如何轉換成PHP數組
呢?
或者,還能有其他辦法來擷取資料嗎?
回複內容:
前台的代碼:
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); });
後端用的PHP
,現在我用file_get_contents("php://input");
能擷取到傳過來的資料,可是拿到的是原始的一個JS 對象
,要如何轉換成PHP數組
呢?
或者,還能有其他辦法來擷取資料嗎?
這樣過去的應該是JSON格式:
http://www.ruanyifeng.com/blog/2011/01/json_in_php.html