jquery Ajax不适用于Spring 3.2.3
我试图学习SpringMVC+jQueryAjax,但我在Ajax成功时遇到了这个错误,下面是我的代码配置 jQuery Ajax。jquery Ajax不适用于Spring 3.2.3,jquery,spring,spring-mvc,Jquery,Spring,Spring Mvc,我试图学习SpringMVC+jQueryAjax,但我在Ajax成功时遇到了这个错误,下面是我的代码配置 jQuery Ajax。 $.ajax({ type:"GET", url:"" +attr.url +"", contentType: "application/json", success:function(response){ console.log("Respon
$.ajax({
type:"GET",
url:"" +attr.url +"",
contentType: "application/json",
success:function(response){
console.log("Response length",response.length);
},
error:function(e){
}
});
弹簧控制器
@RequestMapping(value ="/getList.htm", method= RequestMethod.GET)
public @ResponseBody List<SomeClass> _giveList(HttpServletResponse response) throws DaException{
List<SomeClass>someClass = iSomeClass.getData();
return someClass;
}
控制器方法正在执行,但ajax请求中没有响应。因此,我没有看到ajax成功函数的控制台日志。
但我得到了这个错误
此请求标识的资源只能根据请求“接受”标头生成具有不可接受特征的响应。
我试图通过添加headers=“application/json”来解决这个问题,但运气不好。
任何帮助都是值得的。当spring mvc框架无法将表示(在您的例子中是
json
)从java对象转换为java对象时,您面临的问题是典型的。这可能是由于配置转换器时出错或过程中出错造成的
您的案例是前者,使用spring3.x
您应该删除当前的jackson依赖项,并使用例如
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
org.codehaus.jackson
杰克逊地图绘制者
1.9.13
com.fasterxml.jackson
是用于jackson 2.x
的软件包,Spring 4.x版本正在使用该软件包
对于您当前的代码,其效果与根本没有负责json/java转换的
jackson
依赖项相同,这就是为什么您会收到错误的原因您是否希望响应数据是json格式的
$.ajax({
type:"GET",
url:"" +attr.url +"",
// contentType is the type of data you're sending, Since you're not sending any
// data, I assume you expect the response to be in JSON format, so you need
// to change it to dataType: "json"
contentType: "application/json",
success:function(response){
console.log("Response length",response.length);
},
error:function(e){
}
});
因此,如果您希望数据列表采用JSON格式,那么您可能需要告诉您的控制器它的生成类型。您不需要显式地指定它,因为您已经定义了JacksonObjectMapper,只需尝试一下即可
@RequestMapping(value ="/getList.htm", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
// ^ It's not necessary to specify like this, but just try it
public @ResponseBody List<SomeClass> _giveList(HttpServletResponse response) throws DaException{
List<SomeClass>someClass = iSomeClass.getData();
return someClass;
}
@RequestMapping(value=“/getList.htm”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
//^不必这样指定,只需尝试即可
public@ResponseBody List\u给定列表(HttpServletResponse)抛出DaException{
ListsomeClass=iSomeClass.getData();
返回某个类;
}
我从这篇文章中找到了问题所在。
我用.son替换了.htm,它成功了。我不知道Spring3有什么问题。Jquery是前端,SpringMVC是后端,这与那些框架无关。。您正在返回一个名为shoppingBrandMasters
的东西,我在您的控制器中似乎找不到它..已更改了返回类型。很抱歉犯了这个错误。您可以抽出一些时间,与google jquery ajax和SPRING MVC交流。我的意思是,您的问题似乎要归咎于jquery ajax或SPRING MVC,因为它与另一个不起作用。实际上它正在工作,这就是为什么你会得到错误响应,但是错误是由于用户代码中的问题。我已经试过了,但是没有任何帮助。还修改了POM文件。。。现在您可以看到我使用的jackson依赖项org.codehaus.jackson
deps是您应该为您的版本保留的,因此删除com.fasterxml.jackson
,这肯定是错误的。您还应该检查一件事,即您的SomeClass
是否是有效的POJO,使用erroneus setters/getter会产生相同的错误
$.ajax({
type:"GET",
url:"" +attr.url +"",
// contentType is the type of data you're sending, Since you're not sending any
// data, I assume you expect the response to be in JSON format, so you need
// to change it to dataType: "json"
contentType: "application/json",
success:function(response){
console.log("Response length",response.length);
},
error:function(e){
}
});
@RequestMapping(value ="/getList.htm", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
// ^ It's not necessary to specify like this, but just try it
public @ResponseBody List<SomeClass> _giveList(HttpServletResponse response) throws DaException{
List<SomeClass>someClass = iSomeClass.getData();
return someClass;
}