HTML and CSS Reference
In-Depth Information
else {
sendStandardResponse ( $added );
}
function sendAjaxResponse ( $added ) {
header ( "Content-Type: application/x-javascript" );
if ( $added ) {
header ( 'Status: 201' );
echo ( json_encode ( $added ) );
}
else {
header ( 'Status: 400' );
}
}
function sendStandardResponse ( $added ) {
if ( $added ) {
header ( 'Location: index.php' );
}
else {
header ( 'Location: index.php?error=Your comment was not posted
due to errors in your form submission' );
}
}
?>
Key points from the above code are:
The $db->add_comment($_POST) call returns the data from the added comment which is
assigned to the $added variable.
By setting the response Content-Type to “application/json” we tell jQuery to convert the
returned string into a JavaScript object. For more information on calling JSON Web
services see A Beginner's Guide To jQuery-Based JSON API Clients .
16
The 201 status code indicates a successful call and also that a resource (the comment)
was created by the call.
Search WWH ::




Custom Search