json_decode $_POST 未收到 Javascript JSON
json_decode $_POST not receiving Javascript JSON
我正在尝试将 json 格式的值发送到 php,但是当我调用该函数时,php returns 上的 $_POST 为 null。此过程类似于底部的 w3schools php json 示例 https://www.w3schools.com/js/js_json_php.asp 但仍然没有得到相同的结果。 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
$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);
将内容类型 header 设置为 application/json
。
在使用原始请求的 PHP 中,您不会在 $_POST 中填充变量,而是需要通过原始请求获取 json 才能获取它:
检查请求类型是否为post如果不是则退出
PHP
<?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);
我正在尝试将 json 格式的值发送到 php,但是当我调用该函数时,php returns 上的 $_POST 为 null。此过程类似于底部的 w3schools php json 示例 https://www.w3schools.com/js/js_json_php.asp 但仍然没有得到相同的结果。 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
$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);
将内容类型 header 设置为 application/json
。
在使用原始请求的 PHP 中,您不会在 $_POST 中填充变量,而是需要通过原始请求获取 json 才能获取它:
检查请求类型是否为post如果不是则退出 PHP
<?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);