3

I have two select options and i would like when the user selects one option that other is populated with data from the database. However i am having issues in getting the object list returned to the view.

Controller

@RequestMapping(value="getCrimeTypeList.htm", method = RequestMethod.GET)
 public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{              

        try {
            List<CrimeType> crimeTypeList = this.crimeTypeManager.getCrimeTypeList(crimeCatId);

             return crimeTypeList;
        } catch (Exception e) {

            logger.error(e.getMessage());
            return null;
        }
}

JQuery

 $("select#offenceCatId").change(function(){

       $.ajax({
           type:'GET',
           url:'getCrimeTypeList.htm',
           data:{crimeCatId: $(this).val()},

            headers: {
             Accept: 'application/json'
            },
           dataType: 'json',

           success:function(data){

            alert('it worked');

           }

       });
     });

HTML

<li>
 <label>Offence Type</label>
 <form:select path="offenceTypeId" id="offenceTypeId" title="Offence Type">
 <form:options items="${crimeType.crimeTypeList}" itemValue="crimeTypeId" itemLabel="crimeTypeDesc"/>
 </form:select>
 <form:errors path="offenceTypeId" class="errors" />
</li>

Error

"NetworkError: 400 Bad Request - http://localhost:8084/crimeTrack/getCrimeTypeList.htm?[object%20Object]"

EDITED I did some experimentation and found if the Controller is returning a String it works however once its returning an Object i am having the issues stated.

FireBug

GET http://localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=6 406 Not Acceptable

Response Headers
Content-Length  1067
Content-Type    text/html;charset=utf-8
Date    Fri, 29 Mar 2013 00:58:17 GMT
Server  Apache-Coyote/1.1

Request Headers
Accept  application/json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host    localhost:8084
Referer http://localhost:8084/crimeTrack/crime_registration.htm
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
X-Requested-With    XMLHttpRequest
6
  • Do you have any exceptions there? Commented Mar 28, 2013 at 11:08
  • 3
    Until you have provided details of issues (wrong behaviour, exceptions etc.) I'd like to tell, what is in the top of my head: we faced, that regardless what accept-type you're providing, if you have extension *.htm or *.html on your resource's URL, you won't be getting JSON. Please try to change your URL into f.e. getCrimeTypeList.json or without extension at all. Commented Mar 28, 2013 at 11:25
  • Since you use mvc then everything will be ok, what you should do is telling me what the problem is plz? Commented Mar 28, 2013 at 12:53
  • question updated error included Commented Mar 28, 2013 at 13:23
  • Have you tried hitting the url in browser? is it returning correctly? Commented Mar 28, 2013 at 13:27

2 Answers 2

4

Firstly please make sure your Spring version is 3.1.1 release and you have added jackson.jar in you lib, then try using below code, your code has something reduntant.

@RequestMapping(value="/getCrimeTypeList.htm", method = RequestMethod.GET)
public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{    
            try {
                return this.crimeTypeManager.getCrimeTypeList(crimeCatId);
                //return "true";
            } catch (Exception e) {
                logger.error(e.getMessage());
                return null;
            }
}
$("select#offenceCatId").change(function(){
        var param={crimeCatId:$(this).val()};
        $.ajax({
            type:'GET',
            url:'getCrimeTypeList.htm',
            data:param,
            success:function(data){
                //append options to list
            }
        });
});
Sign up to request clarification or add additional context in comments.

11 Comments

I am getting the following error : ""NetworkError: 406 Not Acceptable - localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=5""
Which version of your Spring? several release of Spring has this issue.
@QQJF i am using spring version 3.1
Ok, if you can please use the version 3.1.1 release, I helped another guy solve this issue yesterday whose version is Spring 3.2
And did you add jackson.jar in your lib?
|
2

Your controller is expecting an request header Accept=application/json, in your case you are not setting it.

Try setting the Accept header

jQuery.ajax({
        type:'GET',
        url:'getCrimeTypeList.htm',
        data:{crimeCatId:$(this).val()},
        processData:false,
        headers: {
            Accept: 'application/json'
        },
        dataType: 'json',
        success:function(data){


            //append options to list


        }

    });

2 Comments

I am getting the following error "NetworkError: 400 Bad Request - localhost:8084/crimeTrack/…"
do you have jackson library in your classpath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.