PHP - 接收检验验证码 , 以JSON的形式将验证结果返回给前端
<?php
session_start();
$codeT = strtoupper(trim($_POST['codeT']));//接收前端传来的数据,转换成大写
$raw_success = array('codeT' => 1, 'msg' => '验证码正确');
$raw_fail = array('codeT' => 2, 'msg' => '验证码错误');
$res_success = json_encode($raw_success);
$res_fail = json_encode($raw_fail);
header('Content-Type:application/json');//这个类型声明非常关键
echo '$codeT : '.$codeT.'<br>';
echo '$_SESSION["sessionCode"]) : '.$_SESSION["sessionCode"].'<br>';
if ($codeT == $_SESSION["sessionCode"]) {
echo $res_success;
} else {
echo $res_fail;
}
?>