gpt4 book ai didi

java - SpringMVC 返回带有 JSON 的对象列表

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:49:40 24 4
gpt4 key购买 nike

我有两个选择选项,我希望当用户选择一个选项时,另一个选项会填充数据库中的数据。但是,我在将对象列表返回到 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>

错误

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

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

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

最佳答案

首先请确保你的 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
}
});
});

关于java - SpringMVC 返回带有 JSON 的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15679879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com