这是我的jquery代码:
var jsObj = {"user_id":5, "login":"hsm"};
var str_json = JSON.stringify(jsObj);
$.post("json_handler.php", str_json)
.done(function(){
alert('data sent successfully');
window.location = "http://localhost/quranMapping/php/json_handler.php";
})
.fail(function(){
alert('fail');
});注意:我进行了重定向,以检查问题出在哪里。
这是我的php代码(json_handler.php文件):
<?php
//create a DB connection
$con=mysqli_connect("127.0.0.1","root","","my_db");
$input = file_get_contents('php://input');
$result = json_decode($input);
echo $result->user_id;
mysql_close($con);
?>当错误地看一看所附图片时,
我如何纠正这样的问题?

更新:当我使用$input = $_POST['str_json']时,错误是undefined index
发布于 2013-08-02 23:16:46
json_decode可能失败,返回NULL值。执行var_dump($result)和var_dump($input)以查看您从客户端和json_decode获得的内容。
发布于 2013-08-02 23:29:18
为什么你不试着使用
JS:
var jsObj = {"user_id":5, "login":"hsm"};
var str_json = JSON.stringify(jsObj);
$.post("json_handler.php", {"str_json" :str_json})
.done(function(){
alert('data sent successfully');
window.location = "http://localhost/quranMapping/php/json_handler.php";
})
.fail(function(){
alert('fail');
});PHP:
<?php
//create a DB connection
$con=mysqli_connect("127.0.0.1","root","","my_db");
$input= $_POST['str_json']
$result = json_decode($input);
echo $result->user_id;
mysql_close($con);
?>https://stackoverflow.com/questions/18020434
复制相似问题