Code Example:
index.html
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function(data) {
var string = JSON.stringify(data);
var string2 = JSON.parse(data);
console.log(typeof data);
console.log(typeof string);
console.log(typeof string2);
console.log(string2.s);
console.log(JSON.stringify(data));
if (data.s) {
alert('you are successful.');
} else {
alert('Sorry, you failed, pls try again later.');
}
console.log(data);
});
});
</script>
</head>
<body>
<form id="myForm" action="comment.php" method="post">
Name: <input type="text" name="name" />
Comment: <textarea name="comment"></textarea>
<input type="submit" value="Submit Comment" />
</form>
</body>
</html>
comment.php
echo json_encode(array('s' => 1, 'data' => $_POST));
JSON.stringfy(value[, replacer[, space]])
JSON.stringfy() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
JSON.parse(text[, reviver])
*The JSON.parse() method parse a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a tranformation on the resulting object before it is returned. *