【问题标题】:SpringMVC Returning a List Of Objects With JSONSpringMVC 使用 JSON 返回对象列表
【发布时间】:2013-03-18 18:36:31
【问题描述】:

我有两个选择选项,当用户选择一个选项时,我希望另一个选项由数据库中的数据填充。但是,我在将对象列表返回到视图时遇到问题。

控制器

@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>

错误

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

已编辑 我做了一些实验,发现控制器是否返回一个字符串它可以工作,但是一旦它返回一个对象,我就会遇到问题。

萤火虫

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

【问题讨论】:

  • 你有什么例外吗?
  • 在您提供问题的详细信息(错误行为、异常等)之前,我想告诉您,我的想法是什么:我们面对的是,无论您接受哪种类型重新提供,如果您的资源 URL 上有扩展名 *.htm*.html,您将不会获得 JSON。请尝试将您的网址更改为 f.e. getCrimeTypeList.json 或根本没有扩展名。
  • 既然你使用mvc那么一切都会好的,你应该做的就是告诉我问题是什么?
  • 包含问题更新错误
  • 你试过在浏览器中点击网址吗?它返回正确吗?

标签: java jquery json spring spring-mvc


【解决方案1】:

您的控制器需要一个请求标头Accept=application/json,在您的情况下您没有设置它。

尝试设置Accept 标头

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】:

首先请确保你的 Spring 版本是 3.1.1 版本并且你已经在你的 lib 中添加了 jackson.jar,然后尝试使用下面的代码,你的代码有一些冗余。

@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
            }
        });
});

【讨论】:

  • 我收到以下错误:""NetworkError: 406 Not Acceptable - localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=5""
  • 你的 Spring 是哪个版本的? Spring 的几个版本都有这个问题。
  • @QQJF 我用的是spring 3.1版
  • 好的,如果可以请使用3.1.1版本,我昨天帮另一个人解决了这个问题,版本是Spring 3.2
  • 你在你的库中添加了jackson.jar吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 2019-07-12
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多