-
Notifications
You must be signed in to change notification settings - Fork 3
Handling HTTP Errors
Eugene edited this page Jun 21, 2021
·
2 revisions
- Set the response header as usual
- Set the message to send back to the client
- Set the HTTP response code
- Return the server's response code
static onion_connection_status handler_root(void *_, onion_request *req, onion_response *res) {
build_response_header(res);
onion_response_printf(res, "SWTbahn server");
onion_response_set_code(res, HTTP_BAD_REQUEST);
return OCS_PROCESSED;
}
jQuery ajax
is used to communicate with the server asynchronously. For the error
handler, the response text is contained in the first parameter of ajax
, which is a jqXHR Object.
$('#pingButton').click(function () {
$('#pingResponse').text('Waiting');
$.ajax({
type: 'POST',
url: '/',
crossDomain: true,
data: null,
dataType: 'text',
success: function (responseData, textStatus, jqXHR) {
$('#pingResponse').parent().removeClass('alert-danger');
$('#pingResponse').parent().addClass('alert-success');
$('#pingResponse').text('OK');
},
error: function (responseData, textStatus, errorThrown) {
$('#pingResponse').parent().removeClass('alert-success');
$('#pingResponse').parent().addClass('alert-danger');
$('#pingResponse').text(responseData.responseText);
}
});
});