Update - 2016-01-30
As advised in the comments I have:
- installed XAMPP
- put my page on Apache and ran the server
- made sure the page and the scripts are executing (simple alert test)
- set the xhrFields: withCredentials to false to make CORS request as advised in the only answer and taught here
I still cannot pass the request. This is the console log:
XMLHttpRequest cannot load http://localhost:8080/RimmaNew/rest/appointments.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 400
Here how it looks on the 'user end' if this is of any help:
------------------Initial ticket-----------------------------
I have a java rest application that runs locally and accepts so far one get method and returns a json string:
Another method is POST, which is supposed to convert, validate and save the information from the sent form and return a 200 answer. If anything goes wrong during conversion, validation or persistence - an exception is thrown (e.g. BadRequest or 500).
This is the rest method (I omitted for brevity validating, build methods as well as the converter and converter provider classes):
@POST
@Produces("application/json")
@Consumes("application/x-www-form-urlencoded")
public Response bookAppointment(@FormParam("date") Date appDate,
@FormParam("time") Time appTime, @FormParam("type") String appType,
@FormParam("clientName") String clientName, @FormParam("email") String clientEmail,
@DefaultValue("") @FormParam("message") String clientMsg) {
//externalize the validation of all fields to concentrate on "positive"
//scenario only
validator(appDate, appTime, appType, clientName, clientEmail);
Appointment appointment = build(appDate, appTime, appType,clientName,
clientEmail, clientMsg);
try {
repository.add(appointment);
return Response.ok(appointment).build();
} catch (Exception e) {
throw new InternalServerErrorException("Something happened in the application "
+ "and this apointment could not get saved. Please contact us "
+ "to inform us of this issue.");
}
}
The client is not within the application (I thought may be this will be useful) - it is a simple HTML file on my computer that has this jQuery script:
<script type='text/javascript'>
$(document).ready(function(){
$('form#appointmentForm').submit(function(ev){
ev.preventDefault();
$.ajax({
url: 'localhost:8080/RimmaNew/rest/appointments',
type: 'post',
dataType: 'json',
data: $('form#appointmentForm').serialize(),
contentType: 'application/x-www-form-urlencoded',
beforeSend: function() {
$('#ajaxResponse').html("<img src='245.gif' />");
},
success: function(data) {
$('#ajaxResponse').html("<span style='color:white; background-color: green;' class='glyphicon glyphicon-ok'></span><p>"+JSON.stringify(data)+"</p>")
.fadeIn("slow");
},
error: function(xhr, ajaxOptions, thrownError){
$('#ajaxResponse').html("<span style='color:white; background-color: red;' class='glyphicon glyphicon-exclamation-sign'></span><p>Status: ").append(xhr.status)
.append("</p>").fadeIn("slow");
}
});
});
});
</script>
I want to submit a form in order to access its params with the @FormParam annotated attributes. On every request that I send I do not receive any of the thrown errors and the status is always 0. Do you see where am I erring or what am I missing?