簡體   English   中英

json_decode $_POST 沒有收到 Javascript JSON

[英]json_decode $_POST not receiving Javascript JSON

我正在嘗試將 json 格式的值發送到 php,但是當我調用該函數時,php 上的 $_POST 返回 null。 此過程類似於在https://www.w3schools.com/js/js_json_php.asp底部找到的 w3schools php json 示例,但仍然沒有得到相同的結果。 json 中字符串旁邊的值是其中包含值的變量。 這是我所做的:

var args = {'name': name, 'birthday': birthday, 'policy': policy, 'cl': cl, 'claim': claim, 'incoming': incoming, 'theDate': theDate,
    'xora': xora, 'treatment': treat, 'session': sess, 'inpat': inpat, 'outpat': outpat, 'daycase': daycase,
    'radioBtn': radioBtn, 'admDate': admDate, 'invDate': invDate, 'invNum': invNum, "nomisma": nomisma,
    'provSelect': provSelect, 'specSel1': specSelect1, 'prescSel': prescSelect, 'specSel2': specSelect2, 'amount': amount,
    'deduct': deduct, 'dedColl': dedColl, 'copay': copay, 'copayColl': copayColl, 'totalAm': totalAm, 'comms': comms,
    'diagnosiDesc': diagnosiDesc, 'diagnosiCode': diagnosiCode, 'nonAmount': nonAmount, 'reason': reason, 'categs': category };
var json = JSON.stringify(args);

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
        //alert("Saved and Continue");
        window.open("PHP/SaveAndCont.php?q="+json); // Test
    }
};

xmlhttp.open("POST","PHP/SaveAndCont.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("q="+json);

這是我的 php 文件:

include('Connection.php');
header("Content-Type: application/json; charset=UTF-8");
$parse = $_POST['q']; // undefined index q
$obj = json_decode($parse, false);
var_dump($parse, $obj); // NULL

將內容類型標頭設置為application/json

在帶有原始請求的 PHP 中,您不會在 $_POST 中填充變量,而是需要通過原始請求獲取 json 來獲取它:

如果沒有退出PHP,檢查請求類型是否為post

<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    die("wrong type of request"); 
}
 // get raw json post object and decode it
 $json = json_decode(file_get_contents("php://input"));
 var_dump($jsonString);

JS:

var args = {'name': name, 'birthday': birthday, 'policy': policy, 'cl': cl, 'claim': claim, 'incoming': incoming, 'theDate': theDate,
    'xora': xora, 'treatment': treat, 'session': sess, 'inpat': inpat, 'outpat': outpat, 'daycase': daycase,
    'radioBtn': radioBtn, 'admDate': admDate, 'invDate': invDate, 'invNum': invNum, "nomisma": nomisma,
    'provSelect': provSelect, 'specSel1': specSelect1, 'prescSel': prescSelect, 'specSel2': specSelect2, 'amount': amount,
    'deduct': deduct, 'dedColl': dedColl, 'copay': copay, 'copayColl': copayColl, 'totalAm': totalAm, 'comms': comms,
    'diagnosiDesc': diagnosiDesc, 'diagnosiCode': diagnosiCode, 'nonAmount': nonAmount, 'reason': reason, 'categs': category };
var json = JSON.stringify(args);

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
        //alert("Saved and Continue");
        alert(xmlhtml.responseText); // this should print the response
    } else if(this.status !== 200) {
        alert(this.status); // maybe it'll be 404 or 500 if so then correct the url in xmlhttp.open it depends on your server configuration but it needs to be accessed via http://localhost/ or host defined by you
    }
};

xmlhttp.open("POST","php/saveandcont.php", true); // this should be path that can be opened via browser
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send(json);

$parse 和 $obj NULL 變量都意味着 $_POST['q'] 未定義。

我認為這個字符串中的問題:

xmlhttp.open("POST","PHP/SaveAndCont.php",true);

第二個參數是一個 URL: https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp但是你將它設置為文件的路徑。 嘗試將其替換為

xmlhttp.open("POST","SaveAndCont.php",true);

或者

xmlhttp.open("POST","http://fullserverurl/SaveAndCont.php",true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM