ajax 请求不允许 - POST 请求
ajax request not allowed - POST request
我想发送 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);
}
});
}
我还有这个服务器端代码来处理请求:
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
}
当我 运行 我的代码时,我在浏览器上看到一个对话框,其中指出:
"ready state:0, response text:"", status:0,statusText:"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.
有人知道我的代码有什么问题吗?
好的。
我的问题是我的服务器 lib 文件夹中没有一些 jar 文件。
以下是丢失文件的名称:
1.twilio-java-sdk-3.3.12.jar。
2. jackson-all-1.9.0.jar.
将它们添加到构建路径,它工作正常!
感谢您的帮助。
我想发送 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);
}
});
}
我还有这个服务器端代码来处理请求:
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
}
当我 运行 我的代码时,我在浏览器上看到一个对话框,其中指出: "ready state:0, response text:"", status:0,statusText:"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.
有人知道我的代码有什么问题吗?
好的。 我的问题是我的服务器 lib 文件夹中没有一些 jar 文件。 以下是丢失文件的名称: 1.twilio-java-sdk-3.3.12.jar。 2. jackson-all-1.9.0.jar.
将它们添加到构建路径,它工作正常!
感谢您的帮助。