简体   繁体   中英

ajax request not allowed - POST request

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM