Spring Jquery Ajax Post 上的 400 错误请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13075101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
400 Bad request on Spring Jquery Ajax Post
提问by Anand Rockzz
I'm getting a 400 bad request on this POST request. Any idea what the issue is here ? Logs are here.
我在这个 POST 请求中收到了 400 个错误的请求。知道这里有什么问题吗?日志在这里。
Controller
控制器
@Controller
public class AjaxController {
@RequestMapping(value="/addKeys", method=RequestMethod.POST, consumes="application/json; charset=UTF-8")
public ResponseEntity<String> addKeys(@RequestParam(value="keys") ArrayList<Keys> keys){
System.out.println("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"+keys);
}
}
context-Servlet.xml
上下文Servlet.xml
<beans>
<mvc:annotation-driven />
<context:component-scan base-package="com.canon.fw.controller" />
<bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingHymansonJsonView" />
</beans>
Ajax
阿贾克斯
tmpList = '[{"key":"camera","label":"Camera"},{"key":"mobile","label":"Mobile"}]';
$.ajax({
type: 'POST',
url: ctx+'/ajx/addKeys',
data: JSON.stringify({"keys": tmpList }),
success: function(r){
if(r.model.status=='success'){
debugger;
//glist.push(elem.key);
//addToList(elem.key, elem.label);
highlightInfoDisc();
}
},
dataType: 'json',
contentType: 'application/json'
});
FireBug - URL
FireBug - 网址
http://localhost:8080/Di/ajx/addKeys
Firebug - Response Headers
Firebug - 响应头
Cache-Control must-revalidate,no-cache,no-store
Content-Length 1384
Content-Type text/html; charset=iso-8859-1
Server Jetty(6.1.26)
Firebug - Request Headers
Firebug - 请求标头
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Content-Length 74
Content-Type application/json; charset=UTF-8
Cookie JSESSIONID=7mymobst47ig1s7uqy2z1fvx4
Host localhost:8080
Referer http://localhost:8080/Di/tiles/entNews.htm
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1
X-Requested-With XMLHttpRequest
Firebug - Source
萤火虫 - 来源
{"keys":[{"key":"canon","label":"Canon"},{"key":"ricoh","label":"Ricoh"}]}
Firebug - Response
Firebug - 响应
"NetworkError: 400 Bad Request - http://localhost:8080/Di/ajx/addKeys"
回答by digitaljoel
You are posting JSON, not form data, but you are trying to read the ArrayList<Keys>
as a form parameter.
您正在发布 JSON,而不是表单数据,但您正在尝试将其ArrayList<Keys>
作为表单参数读取。
Try changing your method signature from:
尝试更改您的方法签名:
public ResponseEntity<String> addKeys(@RequestParam(value="keys") ArrayList<Keys> keys){
to
到
public ResponseEntity<String> addKeys(@RequestBody Keys[] keys){
That may not work since your JSON has an object that has a keys
property that is the list of keys. So you could try to change your ajax post data to something like
这可能不起作用,因为您的 JSON 有一个具有keys
键列表属性的对象。因此,您可以尝试将 ajax 发布数据更改为类似
data: JSON.stringify(tmpList)
so that you are just posting the list instead of wrapping it in another object that has the keys element.
这样您就可以发布列表,而不是将其包装在另一个具有 keys 元素的对象中。