前后端參數(shù)傳遞那點(diǎn)事

  • 傳遞格式
    • 在ajax中的參數(shù)傳遞格式如下
                            data: {
                                Member: that.formMember,
                                '_csrf-backend': "<?= Yii::$app->getRequest()->getCsrfToken();?>",
                                cacheKey: "<?= $cacheKey?>"
                            },

其中 that.formMember為一個(gè)對(duì)象
可以看出:嵌套的對(duì)象會(huì)轉(zhuǎn)換成了數(shù)組的格式,索引數(shù)組的形式也可以存在
一個(gè)鍵如果沒(méi)有對(duì)應(yīng)值,則沒(méi)有數(shù)據(jù)被傳遞
在前端頁(yè)面中采用的數(shù)字類(lèi)型將被轉(zhuǎn)化成string,bool類(lèi)型被轉(zhuǎn)換成string
演示如下:

    test:1,
    test1:'1',
    test2:'',
    test3:null,
    test4:true,

Member[test]:1
Member[test1]:1
Member[test2]:
Member[test3]:
Member[test4]:true

formData中的數(shù)據(jù)如下:

Member[m_photo]:
Member[m_name]:王會(huì)南
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

現(xiàn)在來(lái)看后臺(tái)接收的格式:
可以看出Member為數(shù)組,Member中的m_photo接收到了空字符串,m_native為索引數(shù)組
后臺(tái)接收數(shù)據(jù)如下:

<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'>'王會(huì)南'</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>
  • 總結(jié)

  1. 只要出現(xiàn)在對(duì)象中鍵都將會(huì)被傳遞,沒(méi)有值將會(huì)被作為空字符串傳遞
  2. 在頁(yè)面往服務(wù)器的傳參過(guò)程中,數(shù)字與bool類(lèi)型都將被作為字符串傳遞
  3. 嵌套的對(duì)象將會(huì)被作為數(shù)組形式傳遞
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容