I want to send ajax request to my local server, here is my:
function getNewsFromDB() {
var httpUrl = "http://10.0.0.2:8080/Rosh/" + 'Get_News';
$.ajax({
type: 'POST',
url: httpUrl,
data: '{"MESSAGE_TYPE":"GET_NEWS","VERSION": "1","CITY":"ROSH_HAAIN"}',
dataType: 'json',
crossDomain: true,
success: function(data) {
window.alert("success");
},
error: function(xhr,textStatus,err) {
window.alert(JSON.stringify(xhr) + " ... " + textStatus + " ... " + err);
}
});
}
Also I have this server side code to handle the request:
res.setContentType("application/json");
try
{
res.addHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
res.setHeader("Access-Control-Max-Age", "8080");
res.setCharacterEncoding("utf-8");
res.getWriter().write(response);
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
When I run my code, I see a dialog on the browser that states: "ready state:0, response text:"", status:0,statusText:"error";
I don't know what this dialog means, the request hasn't arrived to my server.
Also I saw this error:
XMLHttpRequest cannot load http://10.0.0.2:8080/Rosh/Get_News. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 404.
Does someone have any idea what is wrong in my code?