【发布时间】:2016-02-11 12:55:45
【问题描述】:
我正在尝试使用 Ajax 发布请求在服务器上发送令牌。这是单独运行的基本 html 代码http://localhost:56471
$.ajax({
type: 'POST',
url: 'http://localhost:8080/sometoken' ,
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
// Handle or verify the server response.
console.log("Result is: " + result);
},
processData: false,
data: sometoken,
});
我在 8080 端口上运行的 servlet 应用程序试图获取令牌
public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException {
String name=req.getParameter("token");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
}
我对 servlet 完全陌生。这是我的第一个 servlet。调用 ajax 帖子时出现错误XMLHttpRequest cannot load http://localhost:8080/sometoken. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56471' is therefore not allowed access.
谁能帮我这个帖子请求没有错误?
谢谢
【问题讨论】:
-
看起来像是 CORS 问题 stackoverflow.com/questions/5863744/same-origin-policy