【发布时间】:2019-06-10 15:15:22
【问题描述】:
我正在尝试对 ajax 中的 servlet 进行简单调用:
$.ajax({
type: 'GET',
url: 'http://myserver/c/myfunction',
cache : false,
data: {key: 'value'},
success:function(){
alert("success");
},
error:function(){
alert("error");
}
});
在 java 中使用我的 servlet 函数:
@RequestMapping(value = "/c/myfunction", method = {RequestMethod.GET})
public synchronized void myfunction(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
}
我收到状态代码 200 OK,但我仍然看到“错误”警报。有什么建议吗?
【问题讨论】:
-
您检查浏览器控制台了吗?此外,您没有发回任何东西,这可能是导致错误的原因。
-
这似乎是因为我使用了两个不同的服务器:“已被 CORS 策略阻止”。我应该如何解决这个问题?
-
在您的后端允许 Cors 标头。
标签: javascript java ajax servlets