我有一個(gè)使用 jQuery 將表單數(shù)據(jù)發(fā)送到 php 的網(wǎng)頁(yè)。我想解碼數(shù)據(jù)數(shù)組并循環(huán)遍歷它,使用 php print_r 顯示四個(gè)值中的每一個(gè)。這是將數(shù)據(jù)發(fā)布到 php 的 jQuery 函數(shù):<script type="text/javascript">function postData() { return $.ajax({ var datastring = $("#echo_test").serialize(); $.ajax({ type: "POST", url: "echo_test.php", data: {post: datastring}, });});}</script>這是 php 函數(shù) echo_test:<?php$obj = json_decode($_POST['post']);print_r($obj['firstname']);?>我認(rèn)為 json_decode 是錯(cuò)誤的,因?yàn)榘l(fā)布的數(shù)據(jù)不是 json 格式。當(dāng)我單擊調(diào)用 jQuery 函數(shù)的按鈕時(shí),php 頁(yè)面在瀏覽器中打開(kāi)但它是空白的,沒(méi)有任何回顯。理想情況下,我會(huì)遍歷 $obj 以在屏幕上顯示四個(gè)值中的每一個(gè)。最終我會(huì)將數(shù)據(jù)發(fā)布到云服務(wù)器上的 Postgres。現(xiàn)在,我正在本地測(cè)試它以驗(yàn)證數(shù)據(jù),因?yàn)槲沂菑?jQuery 將數(shù)據(jù)傳遞給 php 的新手。感謝您對(duì)此的任何幫助。
如何解碼發(fā)布到 php 的 jQuery json 數(shù)據(jù)
函數(shù)式編程
2021-07-01 16:01:03