superagent 使用post的时候会有2次请求
一次请求的类型是OPTIONS
另一种是POST
开始的时候我如何都接受不到post过来的数据
后来仔细核对了浏览器里的信息发现传输数据的方式不是以前的form data而是request payload
然后就上网找如何处理request payload类型的数据
最终像下面这样解决
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE'); if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ header('HTTP/1.1 204 No Content'); header('Server: Cowboy'); header('Content-Length: 0'); header('Connection: keep-alive'); header('Vary: Access-Control-Request-Headers'); header('Access-Control-Allow-Headers: content-type'); header('Date: Fri, 19 Jan 2018 07:57:49 GMT'); header('Via: 1.1 vegur'); }else if($_SERVER['REQUEST_METHOD'] == 'POST'){ $request_body = file_get_contents('php://input'); $data = json_decode($request_body); echo json_encode($result); }