gpt4 book ai didi

php - 将 JSON 发布到 php 脚本服务器端失败

转载 作者:行者123 更新时间:2023-11-29 13:41:30 24 4
gpt4 key购买 nike

这是我的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);
?>

由于错误请查看附图,

我该如何解决这个问题?

enter image description here

更新:当我使用$input = $_POST['str_json']时,错误是未定义索引

最佳答案

json_decode 可能失败,返回 NULL 值。执行 var_dump($result)var_dump($input) 查看从客户端和 json_decode 获得的内容。

关于php - 将 JSON 发布到 php 脚本服务器端失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com