我有以下 jQuery:
$(document).ready(function() {
$('input[type="submit"]').click(function() {
event.preventDefault();
var email = $('.email').val();
$.ajax({
type: "POST",
url: "register_email.php",
data: JSON.stringify({ "email": email }),
dataType: "json",
contentType: "application/json",
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
});
});
email
变量肯定设置好了,我能搞定alert
。
但是,当我使用 PHP 时,这是我的脚本:
<?php
$db = new mysqli("localhost", "...", "...", "...");
if ($db->connect_error) {
echo "Could not connect to database.";
exit;
}
else {
$emerd = json_decode($_POST["email"]);
$db->query("INSERT INTO emails (email) VALUES (' " . $emerd . "')");
echo $emerd;
}
?>
它总是提醒我“null”。为什么它不明白我在发布什么?