前后端参数传递的学习笔记

  • 测试总结说明

  1. 只要出现在对象中键都将会被传递,没有值将会被作为空字符串传递
  2. 在页面往服务器的传参过程中,数字与bool类型都将被作为字符串传递
  3. 嵌套的对象将会被作为数组形式传递
测试案例:
1.传递格式如下:
  • 在ajax中的参数传递格式如下
  •  data: { Member: that.formMember, '_csrf-backend': "<?= Yii::$app->getRequest()->getCsrfToken();?>", cacheKey: "<?= $cacheKey?>" },


  • 其中 that.formMember为一个对象
    可以看出:嵌套的对象会转换成了数组的格式,索引数组的形式也可以存在
    一个键如果没有对应值,则没有数据被传递
    在前端页面中采用的数字类型将被转化成string,bool类型被转换成string
    演示如下:

     test:1,
        test1:'1',
        test2:'',
        test3:null,
        test4:true,
    
    Member[test]:1
    Member[test1]:1
    Member[test2]:
    Member[test3]:
    Member[test4]:true
    formData中的数据如下:
  • Member[m_photo]:
    Member[m_name]:王会南
    Member[m_gender]:woman
    Member[m_birthday]:19930428
    Member[m_nation]:汉族
    Member[m_native][]:130000
    Member[m_native][]:130400
    Member[m_native][]:130406
    Member[m_join_date]:20180120
    Member[m_id_card]:党员35

现在来看后台接收的格式:
可以看出Member为数组,Member中的m_photo接收到了空字符串,m_native为索引数组
后台接收数据如下:

<b>array</b> <i>(size=3)</i>
  'Member' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=25)</i>
      'm_photo' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
      'm_name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'王会南'</font> <i>(length=9)</i>
      'm_gender' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'woman'</font> <i>(length=5)</i>
      'm_birthday' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'19930428'</font> <i>(length=8)</i>
      'm_nation' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'汉族'</font> <i>(length=6)</i>
      'm_native' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=3)</i>
          0 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'130000'</font> <i>(length=6)</i>
          1 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'130400'</font> <i>(length=6)</i>
          2 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'130406'</font> <i>(length=6)</i>
原文地址:https://www.cnblogs.com/xiaohouye/p/11156153.html