我正在尝试将json格式的值发送到php,但是当我调用该函数时,php上的$ _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
答案 0 :(得分:2)
将内容类型标题设置为<?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);
。
在带有原始请求的PHP中,你不会在$ _POST中填充变量,而是需要通过原始请求获取json来获取它:
如果不退出,请检查请求类型是否发布 PHP
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);
JS:
{{1}}
答案 1 :(得分:-1)
$ parse和$ obj NULL变量都表示$ _POST ['q']未定义。
我认为这个字符串中的问题是:
xmlhttp.open("POST","PHP/SaveAndCont.php",true);
第二个参数是一个网址:https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp 但是您将其设置为文件的路径。 尝试将其替换为
xmlhttp.open("POST","SaveAndCont.php",true);
或
xmlhttp.open("POST","http://fullserverurl/SaveAndCont.php",true);