Jquery .ajax() SyntaxError: Unexpected token N
Jquery .ajax() SyntaxError: Unexpected token N
我有以下显示表单的代码,应该通过 Jquery .ajax() 提交表单数据以保存到 MySQL 数据库中。提交表单时,我收到错误 SyntaxError: Unexpected token N
。这是什么意思,我该如何解决?
HTML:
<form class="well" id="jquery-submit-ajax" name="jquery-submit-ajax" method="post" action="ajax.php">
<div class="floatleft">
<input type="text" class="span7" name="yourName" placeholder="Your Name">
</div>
<div class="floatleft">
<input type="text" class="span7" name="yourNumber" placeholder="Your Number">
</div>
<input class="btn btn-primary" type="submit" value="Schedule">
<br /><br />
<div class="alert alert-success hide">
<p>Form successfully submitted! The data sent is below:</p>
<div id="success-output" class="prettyprint"></div>
</div>
<div class="alert alert-error hide">
<p>Error below:</p>
<div id="error-output" class="prettyprint"></div>
</div>
Jquery:
<script>
$("#jquery-submit-ajax").submit(function(e) {
var postData = $(e.target).serialize();
$.ajax({
type: "POST",
url: "ajax.php",
data: postData,
dataType: "json",
beforeSend:function(){
$('.alert-error,.alert-success').hide();
},
error: function(jqXHR, textStatus, errorThrown){
$('.alert-error').fadeIn();
$('#error-output').html(errorThrown);
},
success: function(data){
$('.alert-success').fadeIn();
$('#success-output').html(data);
}
});
return false;
});
</script>
ajax.php:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$yourName = htmlspecialchars(trim($_POST['yourName']));
$yourNumber = htmlspecialchars(trim($_POST['yourNumber']));
$sql = "INSERT INTO contacts (name, number)
VALUES ('$yourName', '$yourNumber')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
我怀疑 JSON 中传递了一个 NaN。 JSON 不支持 NaN。使用 http://jsonlint.com/ 验证通过 postData 传递的 json 数据。
我有以下显示表单的代码,应该通过 Jquery .ajax() 提交表单数据以保存到 MySQL 数据库中。提交表单时,我收到错误 SyntaxError: Unexpected token N
。这是什么意思,我该如何解决?
HTML:
<form class="well" id="jquery-submit-ajax" name="jquery-submit-ajax" method="post" action="ajax.php">
<div class="floatleft">
<input type="text" class="span7" name="yourName" placeholder="Your Name">
</div>
<div class="floatleft">
<input type="text" class="span7" name="yourNumber" placeholder="Your Number">
</div>
<input class="btn btn-primary" type="submit" value="Schedule">
<br /><br />
<div class="alert alert-success hide">
<p>Form successfully submitted! The data sent is below:</p>
<div id="success-output" class="prettyprint"></div>
</div>
<div class="alert alert-error hide">
<p>Error below:</p>
<div id="error-output" class="prettyprint"></div>
</div>
Jquery:
<script>
$("#jquery-submit-ajax").submit(function(e) {
var postData = $(e.target).serialize();
$.ajax({
type: "POST",
url: "ajax.php",
data: postData,
dataType: "json",
beforeSend:function(){
$('.alert-error,.alert-success').hide();
},
error: function(jqXHR, textStatus, errorThrown){
$('.alert-error').fadeIn();
$('#error-output').html(errorThrown);
},
success: function(data){
$('.alert-success').fadeIn();
$('#success-output').html(data);
}
});
return false;
});
</script>
ajax.php:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$yourName = htmlspecialchars(trim($_POST['yourName']));
$yourNumber = htmlspecialchars(trim($_POST['yourNumber']));
$sql = "INSERT INTO contacts (name, number)
VALUES ('$yourName', '$yourNumber')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
我怀疑 JSON 中传递了一个 NaN。 JSON 不支持 NaN。使用 http://jsonlint.com/ 验证通过 postData 传递的 json 数据。