您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

介绍Spring中ajax与后台传输数据的几种方式

2024/3/10 7:40:09发布25次查看
ajax栏目介绍与后台传输数据的方法
推荐(免费):ajax
最近写ajax与后台传输数据的时候碰到了一个问题,我想ajax以json的方式把数据传输个后台,后台用map的形式接收,然后也以map的形式传回数据。可是一直碰到前台报(*)(@415 unsupported media type) 不支持媒体类型错误,然后经过查阅资料终于解决了。这里总结下关于ajax与后台传输数据的几种方式,上面问题的解决方法在本文最后。
1.把数据放到url中传递js:<code>var id = $("#id").val();$.ajax({type: "post",url: "/iftree/people/getpeoplebyid/"+id,//参数放在url中success:function(data){ alert(data);},error:function(xhr, textstatus, errorthrown) {}});</code>
后台:<pre><code>
@requestmapping(value = "getpeoplebyid/{id}")@responsebody public map<string, object> getpeoplebyid(@pathvariable("id") int id) { //@pathvariable("id") 如果参数名与url定义的一样注解可以不用定义("id") system.out.println(id); map<string, object> map = new hashmap<string, object>(); return map; }}
</code></pre>
2.把数据放到data中js:<code>var id = $("#id").val();$.ajax({type: "post",url: "/iftree/people/getpeoplebyid",data: {id:id},success:function(data){ alert(data.result);},error:function(xhr, textstatus, errorthrown) {}});</code>
后台(两个方式):<pre><code>
@requestmapping(value = "getpeoplebyid")@responsebodypublic map<string, object> getpeoplebyid(httpservletrequest request,httpservletresponse response) { int id = integer.valueof(request.getparameter("id")); map<string, object> map = new hashmap<string, object>(); return map;}
</code></pre>
@requestmapping(value = "getpeoplebyid")@responsebodypublic map<string, object> getpeoplebyid(httpservletrequest request,httpservletresponse response) { int id = integer.valueof(request.getparameter("id")); // 这里得到的都是字符串得转换成你需要的类型 map<string, object> map = new hashmap<string, object>(); return map;}
</code>
3.以json传输(就是开头说的情况)js(包含一些常见的ajax参数解释):<code>var id = $("#id").val();$.ajax({type: "post",//请求类型timeout:10000, //设置请求超时时间(毫秒)async:ture,//是否为异步请求cache:false,//是否从浏览器缓存中加载请求信息。url: "/iftree/people/getpeoplebyid",contenttype: "application/json;charset=utf-8",//提交的数据类型data: json.stringify({id:id}),//这里是把json转化为字符串形式datatype: "json",//返回的数据类型success:function(data){$("#name").val(data.result.name);},error:function(xhr, textstatus, errorthrown) {}});});</code>
后台:<pre><code>
@requestmapping(value = "getpeoplebyid", produces = "application/json")@responsebodypublic map<string, object> getpeoplebyid(@requestbody map<string, object> body){ system.out.println(""+body.get("id")); people people = peopleservice.getpeoplebyid(integer.valueof((string)body.get("id"))); map<string, object> map = new hashmap<string, object>(); map.put("result", people); return map;}
</code></pre>
详解:@requestbody
该注解首先读取request请求的正文数据,然后使用默认配置的httpmessageconverter进行解析,把数据绑定要对象上面,然后再把对象绑定到controllor中的参数上。
@responsebody
该注解也是一样的用于将controller的方法返回的对象,通过的httpmessageconverter转换为指定格式后,写入到response对象的body数据区。
srping mvc .xml(配置转换器)<code>
<!-- spring mvc提供的适配器 spring默认加载 (如果不修改默认加载的4类转换器,该bean可不配置)--> <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <!-- 该适配器默认加载以下4类转换器--> <list> <bean class="org.springframework.http.converter.bufferedimagehttpmessageconverter" /> <bean class="org.springframework.http.converter.bytearrayhttpmessageconverter" /> <bean class="org.springframework.http.converter.xml.sourcehttpmessageconverter" /> <bean class="org.springframework.http.converter.xml.xmlawareformhttpmessageconverter" /> <bean class="org.springframework.http.converter.stringhttpmessageconverter" /> <bean class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter"> <property name="supportedmediatypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean><!--这里配置了json转换器支持的媒体类型--> </list> </property></bean>
</code>
bytearrayhttpmessageconverter: 负责读取二进制格式的数据和写出二进制格式的数据;
stringhttpmessageconverter: 负责读取字符串格式的数据和写出二进制格式的数据;
resourcehttpmessageconverter:负责读取资源文件和写出资源文件数据;
formhttpmessageconverter: 负责读取form提交的数据
mappingjacksonhttpmessageconverter: 负责读取和写入json格式的数据;
soucehttpmessageconverter: 负责读取和写入 xml 中javax.xml.transform.source定义的数据;
jaxb2rootelementhttpmessageconverter: 负责读取和写入xml 标签格式的数据;
atomfeedhttpmessageconverter: 负责读取和写入atom格式的数据;
rsschannelhttpmessageconverter: 负责读取和写入rss格式的数据;
项目里面我用到的只有json转换器,所以要导入关于json的包(maven):
<code><dependency><groupid>org.codehaus.jackson</groupid><artifactid>jackson-core-asl</artifactid><version>1.9.11</version></dependency><dependency><groupid>org.codehaus.jackson</groupid><artifactid>jackson-mapper-asl</artifactid><version>1.9.11</version></dependency></code>
同样controller中参数也能以实体类的方式接收数据,
开始一直报(415 unsupported media type)的错误是因为配置文件没有写对也没导入相应的包。
如果有哪里不足或错误的地方望提出,谢谢_
想了解更多编程学习,敬请关注php培训栏目!
以上就是介绍spring中ajax与后台传输数据的几种方式的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product