php跨域&收发json数据

发布于 2019-07-16  21 次阅读


一直在用java,php都忘得差不多了= =

跨域

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: *');

接收json数据

$data = json_decode(file_get_contents('php://input'), true);

返回json数据

function ok($data){
    echo json_encode(array('code'=>0,'data'=>$data));
    exit(0);
}

function fail($errno,$message){
    echo json_encode(array('code'=>$errno,'message'=>$message));
    exit(0);
}