从javascript web服务客户端发送CORS POST请求返回http 405错误
Sending CORS POST request from javascript web-service client return http 405 error
几天前,我实现了一个web服务(客户端"服务器)。客户端发送请求与XMLHttpRequest()
,它是好的。然而,今天我试图改变发送请求从它到jquery的$.ajax();道路
function ajaxQuery(){
$.ajax({
url: 'http://localhost:8080/recommender/sider/drugs',
method:'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({"uris":["SampleName1", "SampleName2", "SampleName3"],"limit":100 }),
xhrFields: {
withCredentials: true
}
}).done(function(data) { console.log(data) });
}
这是我在服务器端使用的过滤器
public class CORSFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if (response instanceof HttpServletResponse) {
HttpServletResponse alteredResponse = ((HttpServletResponse) response);
addCorsHeader((HttpServletRequest) request, alteredResponse);
}
filterChain.doFilter(request, response);
}
private void addCorsHeader(HttpServletRequest request, HttpServletResponse response) {
response.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept, X-Requested-By");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Max-Age", "1728000");
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
不幸的是,当我发送这个POST请求时,我得到了http 405错误,这是我错误部分的屏幕截图。https://www.dropbox.com/s/3wqlqkbnqwnjllx/screenshot%202015 - 08 - 18%——2011.27.29.png?dl=0
我该怎么处理这个问题?
您实际上是在发送GET请求,而不是POST。在$.ajax
中使用了method
。我怀疑您的jQuery版本低于1.9.0,应该使用type
。
$.ajax({
...
type:'POST',
...
根据文档(http://api.jquery.com/jquery.ajax/):
)
method
的别名。如果你使用的是1.9.0之前的jQuery版本,你应该使用type
。
我认为你需要改变数据类型为jsonp
{dataType: 'jsonp'}
相关文章:
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 防止双击执行两次jQuery post请求
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如何使用Javascript获取POST请求填充的元素的值
- NodeJS中POST请求的请求体为null
- 在Fiddler JavaScript中识别POST请求
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 通过AJAX向同一页面发送POST请求,并使用$_POST获取值
- Ajax Post请求不起作用
- 如何在cloud9中向server.js发送post请求
- mootools表单json post请求
- 可以'无法在CasperJS中复制POST请求
- node.js请求POST数组“;第一个参数必须是字符串或缓冲区;
- PythonBottle:访问请求POST数据中的数组变量
- NodeJS请求POST图标
- Yii框架2.0 AJAX请求POST方法