$.ajax与SpringMVC的那点事-传参与返回

$.ajax请求与SpringMVC接收

application-mvc.xml配置javascript

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
            <property name="messageConverters">  
                <list>  
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>  
                </list>  
            </property>
        </bean>  
        
    <!-- 定义跳转的文件的先后缀 ,视图模式配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置个人理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <!--视图解析器配置优先级 -->
        <property name="order" value="1" />
    </bean>

方式一:请求参数为对象,接收以对象接收

$.ajax请求代码java

$.ajax({
	    	url : host + '/utilitiesCharge/queryUtilitiesList.htm',
	    	type:'POST',
	    	dataType:'json',	// 返回数据格式,此配置无关紧要
	    	data: {item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount},
	   	    success:function(data,textStatus,jqXHR){
	   	    	var resp = data; // data为JSON对象	   	    	
	   	    },
	   	    error:function(xhr,textStatus){
	   	        console.log('错误')
	   	        console.log(xhr)
	   	        console.log(textStatus)
	   	    }

SpringMVC接收代码web

@RequestMapping("/queryUtilitiesList")
	@ResponseBody
	public BaseResponse queryUtilitiesList(DLUtilitiesQueryRequest request) {
		logger.info("request params:" + JSONObject.toJSONString(request));
    }

其中,data为对象,ajax

data: {item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}

 

则SpringMVC,可直接用对象接收spring

DLUtilitiesQueryRequest request

方式二:请求以JSON字符串,接收讲JSON字符串转换为对象再接收

$.ajax请求代码json

$.ajax({
	    	url : host + '/utilitiesCharge/queryUtilitiesList.htm',
	    	type:'POST',
	    	dataType:'json',// 返回数据格式,此配置无关紧要
	    	contentType:'application/json',	// 声明请求参数格式为JSON
	    	data: JSON.stringify({item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}),//JSON字符串
	   	    success:function(data,textStatus,jqXHR){

差别部分:mvc

contentType:'application/json',	// 声明请求参数格式为JSON
data: JSON.stringify({item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}),//JSON字符串

SpringMVC接收代码app

@RequestMapping("/queryUtilitiesList")
	@ResponseBody
	public BaseResponse queryUtilitiesList(@RequestBody DLUtilitiesQueryRequest request) {
		logger.info("request params:" + JSONObject.toJSONString(request));
    }

差别部分:jsp

@RequestBody DLUtilitiesQueryRequest request

 

SpringMVC返回与$.ajax接收

各项代码参考上面url

  1. 若是返回的是一个网页内容,则@ResponseBody不用声明,返回字符串(即页面地址如:return "/web/xxx")
  2. 若是返回的是一个JSON,则要声明@ResponseBody:
  • 若是返回的是一个JSON字符串,则ajax的success中的data参数为字符串,
  • 若是返回的是JSON对象,则data为对象。
  • 因为有@RresponseBody标示,SpringMVC会将头信息中的Content-Type:application/x-www-form-urlencoded(默认值)改成application/json;,即返回为JSON格式,因此ajax的dataType:"json"无关紧要。
相关文章
相关标签/搜索