search

Home  >  Q&A  >  body text

serializers - ajax传递过去的data是一个序列化的字符串,为什么php接受过去确实一个数组?

前端代码:
if(register_flag){ //注册信息都正确

        //alert(typeof($('.register-form').serialize()));
        //序列化的结果:name=hesisi&account=15223306809&verification-code=2333&password=hss123456&confirm-pwd=hss123456
        $.ajax({
            type : 'POST',
            url : 'php/register.php',
            data : $('.register-form').serialize(),  //序列化的字符串
            success : function(data){
                //window.location.href = "index.html";
            },
            error : function(){
                //错误信息处理
                console.log();
            }

        });            

    }

php代码
require 'config.php';


$data =$_POST;
//name=hesisi&account=15223306809&verification-code=2333&password=hss123456&confirm-pwd=hss123456
$arr = explode("&",$data);
$name_arr = explode("=",$arr[0]);
$account_arr = explode("=",$arr[1]);
$verifcode_arr = explode("=",$arr[2]);
$password_arr = explode("=",$arr[3]);
$confirmpwd_arr = explode("=",$arr[4]);

$name = $name_arr[1];
$account = $account_arr[1];
$verifcode = $verifcode_arr[1];
$password = $password_arr[1];
$confirmpwd = $confirmpwd_arr[1];

$mobile_code = $_SESSION['mobile_code'];

$query = "SELECT * from user WHERE user_account=".$account;
$result = mysqli_query($query);

if($verifcode != $mobile_code){//手机验证码错误
    exit("手机验证码错误!");
    return;
}else if($result){
    exit("改手机号已经注册!");
    return;
}else{
    $insert = "INSERT INTO user(user_name,password,user_account) VALUES(".$name.",".$password.",".$account.")";
    mysqli_query($insert);
    exit("注册成功!");
}

这里报错说,explode()第二个参数应该是string类型,但是我穿的是数组类型。ajax传过去的data是string类型啊,为什么php通过$_POST[]接受就是array类型了?data之前没写过php,请指教,谢谢~

伊谢尔伦伊谢尔伦2738 days ago972

reply all(4)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:02:09

    The parameter you receive is an array,

    $data =$_POST;
    其实就是下面的数组
    $data['name']='hesisi';
    $data['account']='15223306809';
    $data['verification-code']='2333';
    ...
    并不是name=hesisi&account=15223306809&verification-code=2333&password=hss123456&confirm-pwd=hss123456,
    你这个data就是数组,不是字符串,你不能去explode去切割
    

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:02:09

    ajax adds parameter Content-Type: 'text/plain'

    If php accepts it, don’t use $_POST, change it to file_get_contents('php://input')

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:02:09

    With ajax, no matter whether the data you pass to the backend is json or serialized string, it will be parsed into an array form when it reaches the backend.
    So

    $data =$_POST;
    
    //$data 就是个数组啊 

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:02:09

    I want to visit the original poster, please look at the url address www.baidu.com?search=keyword&s=key&time=143032423

    Do you need to use $_GET when receiving in the background? It is still an array. The key is how $_GET and $_POST work

    reply
    0
  • Cancelreply