$.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"無關緊要。
相關文章
相關標籤/搜索