javascript - Jquery .ajax() SyntaxError: Unexpected token N -


i have following code displays form , supposed submit form data via jquery .ajax() saved mysql database. on submitting form, error syntaxerror: unexpected token n. mean , how can resolve it?

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 submitted! data sent 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 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();  ?> 

i suspect there nan being passed in json. nan unsupported in json. use http://jsonlint.com/ validate json data passed via postdata.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -