NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
#### 前端请求代码: ``` let headres = { method: 'post', credentials: 'include', headers: { contentType: "application/json;charset=utf-8", }, body: JSON.stringify(data) } fetch('/addCompared', headres).then(x => x.json()).then(x => { console.log(x); }).catch(err => { console.error(err); }) ``` #### 后端php处理数据: ``` $data = file_get_contents('php://input'); $data = json_decode($data, true);//第二个参数true,使得$data变为array类型否则会出现以下错误 //Fatal error: Cannot use object of type stdClass as array in //解决办法 $result = json_decode($data, true); ``` ### php处理json.stringify过来的数据 ``` //php $data = I('post.data'); $data = json_decode(htmlspecialchars_decode($data), true); //js let dataFrom = new FormData(); dataFrom.append('data', JSON.stringify(values)); let headres = { method: 'post', credentials: 'include', headers: {}, body: dataFrom } fetch(url, headres).then(x => x.json()).then(x => { console.log(x) }).catch(err => { console.error(err); }) ```