I have a jersey web server add I want to make a post request from javascript:
heartRateTimestamp = new Date().toISOString();
var client = new XMLHttpRequest();
var url = "http://192.168.0.226:8080/appdata/post";
console.log(url);
client.open("post", url);
client.setRequestHeader('Content-Type', 'application/json');
var parameters = [
{
"type": 2,
"HeartRate": {
"heartrate": 65,
"heartratesTimestamp": heartRateTimestamp,
"macAddress": "lsjfsf2s8vev8es"
}
}
];
client.send(JSON.stringify(parameters));
client.onload = function () {
console.log(client.responseText);
document.getElementById("response").innerHTML = "Messages: " + client.responseText;
}
When I try to do this I get an error:
XMLHttpRequest cannot load http://192.168.0.226:8080/appdata/post. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I think this a CORS problem so I followed this tutorial (http://www.codingpedia.org/ama/how-to-add-cors-support-on-the-server-side-in-java-with-jersey/) to overcome this but unfortunately I am still getting the same error. What could be the problem?