我正在尝试在访问给定URL时将控制器中的json列表发送到javascript。使用此代码,当我转到URL时,我收到406错误。我的servlet上下文有<annotation-driven />
,我有org.codehaus.jackson
maven依赖项。有没有其他原因这给我一个406,或者有更好的方法去发送一个列表到javascript?
控制器功能
@RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public @ResponseBody ArrayList<Integer> getHandHygienePageAsync(
@PathVariable(value = "room") String roomCode) {
ArrayList<Integer> json = new ArrayList<Integer>();
Room room = getRoom(roomCode);
json.add(service.getHandHygieneEvents(room));
if (room.isGroupBased()) {
json.add(service.getRoomEntryExits(room));
}
if (room.isIdBased()) {
json.add(service.getPatientContacts(room));
}
return json;
}
的Javascript
$.getJSON(currentURL + ".json",
function(data){
alert('I can\'t get here though');
var overallRatio;
var handWash = data[0];
$("#handwash").html(handWash);
if(groupBased == true)
{
var enex = data[1];
$("#enex").html(enex);
overallRatio = "" + Math.round((handWash*100)/enex) + "%";
}
else
{
if(idBased == true)
{
var contacts = data[2];
$("#contacts").html(contacts);
overallRatio = "" + Math.round((handWash*100)/contacts) + "%";
}
}
$("#overall").html(overallRatio);
}
);
这是请求的标题
GET /groupbased/ICU6/handhygiene.json HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
Referer: http://localhost:8080/groupbased/ICU6/handhygiene
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=063CDE4BF5FB7369B6C48FC2EADFD8E9
此视图解析器是否与任何内容发生冲突?
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
答案 0 :(得分:0)
将以下配置添加到mvc-servet.xml
<bean name="customViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="/WEB-INF/config/spring/customViews.xml" />
<property name="order" value="0"/>
</bean>
在路径/WEB-INF/config/spring/customViews.xml中创建以下customViews.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
</beans>
控制器更改: -
@RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public @ResponseBody String getHandHygienePageAsync(
@PathVariable(value = "room") String roomCode,ModelMap model) {
ArrayList<Integer> json = new ArrayList<Integer>();
Room room = getRoom(roomCode);
json.add(service.getHandHygieneEvents(room));
if (room.isGroupBased()) {
json.add(service.getRoomEntryExits(room));
}
if (room.isIdBased()) {
json.add(service.getPatientContacts(room));
}
model.addAttribute("json",json)
return "jsonView";
}
如果这样可以解决您的问题,请随意将其标记为答案并单击上行链接。