我想将一个 json 对象发布到 php。
var user = {username:"test", password:"test", name:"test",
email:"test@hotmail.com"};
var str_json = JSON.stringify(user);
$.ajax({
url: '/register_API.php',
type: 'post',
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log('success');
},
data: user
});
}
在 php 中我想将它插入到 mysql 中:
$data = file_get_contents('php://input');
$json = json_decode($data,true);
$username = $json['username'];
$password = $json["password"];
$email = $json['email'];
$insertSql = "INSERT INTO users (username, password, email)
VALUES ('$username', '$password', '$email');";
$data 字符串包含:username=test&password=test&name=test&email=test%40hotmail.com,但我无法通过解码获取变量...
提前致谢!
最佳答案
将data: user
改为data: str_json
然后
更改 $data = file_get_contents('php://input');
到 $data = $_POST['data']
关于php - 解码 php 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906970/