我如何对 jQuery 序列化的表单进行 PHP 反序列化?

2021-12-21 00:00:00 serialization jquery php

使用$('#form').serialize(),我能够将其发送到PHP 页面.现在我如何在 PHP 中反序列化它?它是在 jQuery 中序列化的.

Using $('#form').serialize(), I was able to send this over to a PHP page. Now how do I unserialize it in PHP? It was serialized in jQuery.

推荐答案

您不必通过 jquery serialize 方法对 PHP 中的任何内容进行反序列化.如果您序列化数据,如果您使用 GET 方法 ajax 请求,则应将其作为查询参数发送到 PHP,如果您使用 POST ajax 请求,则应将其作为查询参数发送到 PHP.因此,在 PHP 中,您将根据请求类型访问诸如 $_POST["varname"]$_GET["varname"] 之类的值.

You shouldn't have to unserialize anything in PHP from the jquery serialize method. If you serialize the data, it should be sent to PHP as query parameters if you are using a GET method ajax request or post vars if you are using a POST ajax request. So in PHP, you would access values like $_POST["varname"] or $_GET["varname"] depending on the request type.

serialize 方法只是获取表单元素并将它们放在字符串形式中."varname=val&var2=val2"

The serialize method just takes the form elements and puts them in string form. "varname=val&var2=val2"

相关文章