Announcement Announcement Module
Collapse
No announcement yet.
HTTP status 406 - Please HELP!!! Spring+Ajax Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTTP status 406 - Please HELP!!! Spring+Ajax

    Hi,

    I'm using SpringMVC for my web application. In one of the JSPs I'm making an Ajax call using jQuery and retrieving a list of objects.

    my jsp calls
    Code:
    <script type="text/javascript">
    function getCities() {
    	jq(function() {
    		jq.post("getCities.html",
    					{ 	stateSelect:  jq("#stateSelect").val()},
    						function(data){
    							jq("#cities").replaceWith('<span id="cities">Changed</span>');
    					});
    	});
    }
    </script>
    The getCities is mapped to the following:
    Code:
    @RequestMapping(value = "/getCities", method = RequestMethod.POST)
        public @ResponseBody List<StateNames> getCities(@RequestParam(value="stateSelect", required=true) String stateName,
        							Model model) {
    		// Delegate to service to do the actual adding
    		List<StateNames> listStates = myService.listCityNames(stateName);
    		
    		// @ResponseBody will automatically convert the returned value into JSON format
    		// You must have Jackson in your classpath
    		return listStates;
    	}
    I get the following error when I run this.

    Code:
    406 Not Acceptable
        The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
    Please guide me.
    I used Firbug to see what's going on. Here's what i get:
    Code:
    Response Headers
    Server	Apache-Coyote/1.1
    Content-Type	text/html;charset=utf-8
    Content-Length	1070
    Date	Sat, 12 Feb 2011 13:09:44 GMT
    
    Request Headers
    Host	localhost:8080
    User-Agent	Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
    Accept	*/*
    Accept-Language	en-us,en;q=0.5
    Accept-Encoding	gzip,deflate
    Accept-Charset	ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive	115
    Connection	keep-alive
    Content-Type	application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With	XMLHttpRequest
    Referer	http://localhost:8080/MyApplication/
    Content-Length	17
    Cookie	JSESSIONID=640868A479C40792F8AB3DE118AF12E0
    Pragma	no-cache
    Cache-Control	no-cache
    Please guide me.

  • #2
    I suppose you're using the wrong JQuery function since the accept header
    Code:
    Accept: application/json, text/javascript, */*
    is missing, use postJSON() instead.

    Robin.

    Comment


    • #3
      Originally posted by robin View Post
      I suppose you're using the wrong JQuery function since the accept header
      Code:
      Accept: application/json, text/javascript, */*
      is missing, use postJSON() instead.

      Robin.
      I've also tried with postJSON, but I guess that's not the problem. I keep getting the same error.

      I racked my brains for the whole day & came to realize that if i send String, Integer as replies from my controller back to the jsp, the whole thing works well.
      But the time I start sending objects/List of Objects, the code breaks with HTTP 406?
      Why is it so?
      Please help.

      Comment


      • #4
        I also tried the getJSON function and got the same HTTP 406 error:

        Code:
        Response Headers
        Server	Apache-Coyote/1.1
        Content-Type	text/html;charset=utf-8
        Content-Length	1070
        Date	Sun, 13 Feb 2011 14:38:16 GMT
        
        Request Headers
        Host	localhost:8080
        User-Agent	Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
        Accept	application/json, text/javascript, */*; q=0.01
        Accept-Language	en-us,en;q=0.5
        Accept-Encoding	gzip,deflate
        Accept-Charset	ISO-8859-1,utf-8;q=0.7,*;q=0.7
        Keep-Alive	115
        Connection	keep-alive
        x-requested-with	XMLHttpRequest
        Referer	http://localhost:8080/Auditions/
        Cookie	JSESSIONID=62D82E42596B6C8E31EEC76AF28EA81E

        Comment


        • #5
          In order to isolate the problem I would start with allowing a GET request for that method and return a single StateNames instance. Then test the returned value by calling the URL using:
          Code:
          curl -X GET -H "Content-Type: application/json" http://localhost.../getCities
          Do you see json content or not?
          Then I would change the return value back to a list. I'm not sure whether this is supported out of the box, I'm am using a list of values but only wrapped in a return object, not in the return value itself. I can't remember if the reason was that it isn't supported or if this is just an coincendence.

          Comment

          Working...
          X