這是我的jquery
代碼:發佈JSON到PHP腳本服務器端失敗
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
你得到json_decode任何錯誤? http://php.net/manual/en/function.json-last-error.php – JimL
以後參數爲: $ input = $ _POST ['str_json']; – Aditya
哪條線是17號線? –