我想将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']