序列化serialize() 通过ajax将数据传入后台

37 篇文章 1 订阅
26 篇文章 0 订阅

我们先了解一下什么是序列化,看了一篇文章,在PHP中的作用是可以将我们的数组转为对象。

反序列化那么就是将对象在转为数组。

例子看一下可以       ---->  通过ajax将表单值传入后台请看后面(可跳过这里内容)

$sites = array('Google'=>'$ser', 'Runoob'=>'1111', 'Facebook'=>333);
//序列化
$serialized_data = serialize($sites);
//得到   a:3:{s:6:"Google";s:4:"$ser";s:6:"Runoob";s:4:"1111";s:8:"Facebook";i:333;}

//反序列化
$a = unserialize($serialized_data);

//得到
//  array(3) {
//   ["Google"]=>
//   string(4) "$ser"
//   ["Runoob"]=>
//   string(4) "1111"
//   ["Facebook"]=>
//   int(333)
//  }

 

通过ajax将数据传入后台

这里的后台我用的是thinkphp没用用原生php。

前端HTML内容:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <title>Document</title>
</head>

<body>
    <form method="post" name="forms">
        <input type="checkbox" name="a" value="1">
        <input type="checkbox" name="b" value="2">
        <input type="checkbox" name="c" value="3">
        <input type="checkbox" name="d" value="4">
        <input type="checkbox" name="e" value="5">
        <input type="checkbox" name="f" value="6">
        <input type="checkbox" name="g" value="7">
        <button onclick="ceshi()" type="button">tijiao</button>
    </form>
</body>

</html>

<script>
    function ceshi() {
        $.ajax({
            type: 'POST',
            url: "{:url('index/index/test')}",
            data: $('form').serialize(),
            dataType: 'json',
            success: function (data) {
                console.log(data);
            },
            error: function () {
                console.log("error");
            }
        });

    }
</script>

 

index控制器内容:

<?php
namespace app\index\controller;
use think\Log;
use think\Controller;
use think\Db;
class Index extends Controller
 {
   public function test()
    {
        $all = input();
        return json($all);
    }
}

这样我们将多选框的数据返回给了前端。这里返回前端的是一个对象的形式。

下面我们将数据转为数组的格式

tp控制器代码

<?php
namespace app\index\controller;
use think\Log;
use think\Controller;
use think\Db;
class Index extends Controller
 {
    public function test()
    {
            'a'=>input( 'a' ),
            'b'=>input( 'b' ),
            'c'=>input( 'c' ),
            'd'=>input( 'd' ), 
            'e'=>input( 'e' ), 
            'f'=>input( 'f' ), 
            'g'=>input( 'g' ), 
        ];
        return json($data);
    }
}

 

前端处理数据为

<script>
    function ceshi() {
        $.ajax({
            type: 'POST',
            url: "{:url('index/index/test')}",
            data: $('form').serialize(),
            dataType: 'json',
            success: function (data) {
                console.log(data.a);
                console.log(data.b);
                console.log(data.c);
                console.log(data.d);
                console.log(data.e);
                console.log(data.f);
                console.log(data.g);
                
            },
            error: function () {
                console.log("error");
            }
        });

    }
</script>

 

序列化接收值的格式为

c=3&g=7&a=1&d=4&b=2&f=6&e=5

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows Forms 应用程序中,可以使用 .NET Framework 中的序列化功能来将数据保存到本地。 要使用序列化,需要做以下几件事: 1. 将要序列化的对象实现 `ISerializable` 接口。这个接口要求你实现一个 `GetObjectData` 方法,用于将对象的数据写入一个 `SerializationInfo` 对象中。 2. 创建一个 `BinaryFormatter` 对象,用于执行序列化和反序列化操作。 3. 创建一个文件流,用于将数据写入本地文件。 4. 使用 `BinaryFormatter.Serialize` 方法将要序列化的对象写入文件流。 下面是一个示例代码,展示了如何将一个对象序列化到本地文件中: ``` [Serializable] public class MyClass : ISerializable { public string SomeData { get; set; } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("SomeData", SomeData); } } // 序列化对象 MyClass obj = new MyClass { SomeData = "Hello, world!" }; using (FileStream stream = new FileStream("data.bin", FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } ``` 要将数据从本地文件中反序列化回对象,可以使用 `BinaryFormatter.Deserialize` 方法。示例代码如下: ``` using (FileStream stream = new FileStream("data.bin", FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); MyClass obj = (MyClass)formatter.Deserialize(stream); } ``` 请注意,序列化是一种将对象的状态转换为字

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值