简体   繁体   English

不允许ajax请求-POST请求

[英]ajax request not allowed - POST request

I want to send ajax request to my local server, here is my: 我想将ajax请求发送到本地服务器,这是我的:

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"; 当我运行代码时,我在浏览器上看到一个对话框,内容为:“就绪状态:0,响应文本:””,状态:0,statusText:“错误”;

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 . XMLHttpRequest无法加载http://10.0.0.2:8080/Rosh/Get_News No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8000 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:8000 '。 The response had HTTP status code 404. 响应的HTTP状态码为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. 我的问题是我的服务器lib文件夹中没有一些jar文件。 Here is the names of the losted files: 1. twilio-java-sdk-3.3.12.jar. 这是丢失的文件的名称:1. twilio-java-sdk-3.3.12.jar。 2. jackson-all-1.9.0.jar. 2. jackson-all-1.9.0.jar。

Add them to the build path and it's working fine! 将它们添加到构建路径,一切正常!

Thank you for your helps. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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