我想发送一个带POST的JSON对象
var user = {
"first_name" : first_name.value,
"last_name" : last_name.value,
"age" : age.value,
"email" : email.value
};
这是我发送对象的方式:
var request;
var url = 'ajax_serverside_JSON.php';
var destination;
function ajax_call(src, jsonObj, dst, method) {
this.src = src;
destination = dst;
this.objJSON = jsonObj;
this.request = getXMLHttpRequest();
this.request.onreadystatechange = getResponse;
if (method == 'POST') {
sendPostRequest(objJSON);
}
return;
}
function sendPostRequest(objJSON) {
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
request.send(JSON.stringify(objJSON));
return;
}
function getXMLHttpRequest() {
try {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera,Safari
request = new XMLHttpRequest();
} else {
// code for IE6, IE5
request = new ActiveXObject("Microsoft.XMLHTTP");
}
} catch (e) {
alert('The browser doesn\'t support AJAX: ' + e);
request = false;
}
return request;
}
function getResponse() {
if (request.readyState == 4 && request.status == 200) {
document.getElementById(destination).innerHTML += request.response;
}
}
问题:如何在服务器端读取对象php?发送的json的关键是什么?
echo var_dump ( $_POST )
;显示
array (size=0)
empty
有什么问题?
编辑后
我将代码更改为:
this.objJSON = jsonObj;
和
request.send("user=" + JSON.stringify(this.objJSON));
我尝试阅读JSON obj:
<?php
if (isset ( $_POST ['user'] )) {
$obj = json_decode ( $_POST ['user'] );
echo "isset";
} else {
echo "is not set ";
echo var_dump ( $_POST );
}
?>
......仍然是同样的事情:
is not set
array (size=0)
empty
答案 0 :(得分:2)
将你的功能分解为核心3行:
function ajax_call(src, jsonObj, dst, method) {
jsonOBJ
在本地范围内设置为变量。
this.objJSON = jsonObj;
jsonOBJ
已复制到this.objJSON
。
sendPostRequest(objJSON);
仍然没有局部变量objJSON
,因为Javascript不会自动考虑本地范围的this
部分,因此会发送null,因此为空$_POST
。
发送jsonOBJ
或this.objJSON
代替它,它会正常工作。
答案 1 :(得分:2)
尝试:
request.send("obj="+JSON.stringify(objJSON));
它是一个键值对,我猜你错过了键。
答案 2 :(得分:2)
我不确定PHP是否可以自动将JSON解析为$_POST
个变量。您应该将JSON放入普通POST变量的值:
request.send('data=' + encodeURIComponent(JSON.stringify(objJSON));
并使用默认的Content-Type。然后在PHP中你可以这样做:
var_dump(json_decode($_POST['data']));
您还需要修复Javascript变量,如Niels Keurentjes的回答中所述。
答案 3 :(得分:2)
对于您的原始Ajax请求,您必须从php://input
读取请求正文并对其进行解码。
<?php
$post = json_decode(file_get_contents('php://input'));
if (isset ( $post ['user'] )) {
echo "isset";
} else {
echo "is not set ";
echo var_dump ( $post );
}
答案 4 :(得分:1)
<script>
var user = {
"first_name" : first_name.value,
"last_name" : last_name.value,
"age" : age.value,
"email" : email.value
};
$.customPOST = function(data,callback){
$.post('your_php_script.php',data,callback,'json');
}
$(document).ready(function() {
//suppose you have a button "myButton" to submit your data
$("#myButton").click(function(){
$.customPOST(user,function(response){
//on the server side you will have :
//$_POST['first_name'], $_POST['last_name'], .....
//server will return an OBJECT called "response"
//in "index.php" you will need to use json_encode();
//in order to return a response to the client
//json_encode(); must have an array as argument
//the array must be in the form : 'key' => 'value'
});
return false;
});
</script>