0

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?

Neo
  • 3,309
  • 7
  • 35
  • 44
foo
  • 443
  • 2
  • 8
  • 22
  • The response does not have the `Access-Control-Allow-Origin` header set. Your code appears to include it, so double check you're doing it correctly. – Rory McCrossan Nov 02 '16 at 11:13
  • How is the server implemented? –  Nov 02 '16 at 11:14
  • Can you add it in answer please? I don't now how and where to add it... – foo Nov 02 '16 at 11:14
  • if you are doing local to local ajax make sure you have the same url/ip on both the browser address bar and ajax url – madalinivascu Nov 02 '16 at 11:15
  • Not sure if a duplicate, but search SO, there's lots of questions going to CORS in detail: https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work – freedomn-m Nov 02 '16 at 11:15
  • Lutz Horn didnt understood your question... you mean which language? – foo Nov 02 '16 at 11:15
  • @freedomn-m I tried many questions but nothing.... this is the cause that my question wrote. – foo Nov 02 '16 at 11:17
  • try with var httpUrl = "Rosh/" + 'Get_News'; instead of complete url – Viraj Dhamal Nov 02 '16 at 11:22
  • @ Viraj Dhamal sorry but I got error:404 not found... – foo Nov 02 '16 at 11:23
  • @foo: Please post server side code in detail – Viraj Dhamal Nov 02 '16 at 11:26
  • I posted it... which code you need I don't understand.? – foo Nov 02 '16 at 11:27
  • @foo trying opening your page as "http://10.0.0.2:8080" rather than "http://localhost:8000" - assuming 10.0.0.2 *is* your "local server" (as per question). The problem is simply: You can't access 10.0.0.2 from localhost - the hostnames must match. If you want to be able to, then the linked question shows how to do this. – freedomn-m Nov 02 '16 at 11:28

1 Answers1

0

Ok. My problem was that I didn't have some jar files in my server lib folder. Here is the names of the losted files: 1. twilio-java-sdk-3.3.12.jar. 2. jackson-all-1.9.0.jar.

Add them to the build path and it's working fine!

Thank you for your helps.

foo
  • 443
  • 2
  • 8
  • 22