Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

400 Bad request on Spring Jquery Ajax Post

I'm getting a 400 bad request on this POST request. Any idea what the issue is here ? Logs are here.

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

<beans>
    <mvc:annotation-driven />
    <context:component-scan base-package="com.canon.fw.controller" />
    <bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</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

http://localhost:8080/Di/ajx/addKeys

Firebug - Response Headers

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

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

"NetworkError: 400 Bad Request - http://localhost:8080/Di/ajx/addKeys"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are posting JSON, not form data, but you are trying to read the ArrayList<Keys> as a form parameter.

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

data: JSON.stringify(tmpList)

so that you are just posting the list instead of wrapping it in another object that has the keys element.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...