[英]ajax json php decode
i'm looking for passing params between server and client but i don't find a solution. 我正在寻找服务器和客户端之间传递params但我找不到解决方案。 I've read all the web but i don't find a working solution.
我已阅读所有网页,但我找不到有效的解决方案。
Client Side 客户端
function move(column, line) {
$.ajax({
type: 'POST',
url: 'index.php?action=setpawn',
data: {column: column, line: line},
contentType: "application/json; charset=utf-8",
async: false,
success: function(data) {
alert(data.donnees);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown+textStatus);
}
});
}
Server Side 服务器端
$data = json_decode(file_get_contents('php://input'),true);
header('Content-Type:application/json; charset=utf-8');
$response = array('isvalid' => "0" , 'donnees' => $data['column']);
echo json_encode($response);
I can't acces 'column' and 'line' data on the server, and the json response is bad too (sometimes parse error or bad result). 我无法访问服务器上的“列”和“行”数据,并且json响应也很糟糕(有时会解析错误或结果不好)。
Please help me i can't find a way to make it work... (sorry for my bad english, i'm french and i do the best i can) 请帮助我,我找不到让它工作的方法......(抱歉我的英语不好,我是法国人,我尽我所能)
jQuery does not JSON-encode by default, but as x-www-form-urlencoded
. jQuery默认情况下不进行JSON编码,而是
x-www-form-urlencoded
。 You need to send 你需要发送
$.ajax({
type: 'POST',
url: 'index.php?action=setpawn',
data: JSON.stringify({column: column, line: line}),
// ^^^^^^^^^^^^^^
contentType: "application/json; charset=utf-8"
})
Check at the server side whether file_get_contents('php://input')
is what you expect, before json_decode
ing it and getting a parse error. 在
json_decode
之前检查file_get_contents('php://input')
是否符合预期,并获得解析错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.