下拉列表数据动态加载 springmvc jquery ajax

1、Spring-mvc 代码如下 :


<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="cacheSeconds" value="0" />  
        <property name="messageConverters">  
            <list>  
           		 <ref bean="mappingJacksonHttpMessageConverter"/>
            </list>  
        </property>
    </bean>  



 2、Controller 代码如下:

@RequestMapping(value="/dynamicSQLBySelect")
	public @ResponseBody List<PriRole> dynamicSQLBySelect(String queryParameter) throws IOException{
		List<PriRole> selectList = null;
		try {
			if (!"".equals(queryParameter) || queryParameter != null) {
				selectList = formSubjectService.dynamicSQL(queryParameter);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return selectList;
	}

3、页面代码如下:

<select name="xxxxxx" id="onSelect"  >
<option value="">==请选择 ==</option>
</select>


<script type="text/javascript">
		$(document).ready(function() {

			 var url = "formSubjectController/dynamicSQLBySelect.do";
									    $.ajax({
									    	type:"get",
									    	cache:false,
									    	url:url,
									    	data:{queryParameter:'${ptlGlbCfg.queryParameter}'},
									    	error:function(xhr,error,msg){
													alert(xhr);
													alert(error);
													alert(msg);
										    	},
									    	success:function(data){
										    	 var info = '';	
											    	<%--
				  								     $.each(data,function(index,role) { 
				  								      info += "<option value='" + role.roleId + "'>" +  role.roleName + "</option>";
				  								   });--%>
				  								  var temp = eval(data);  
												 for (var i = 0; i < temp.length; i++) {  
												 	info += "<option value='" + temp[i].roleId + "'>" + temp[i].roleName + "</option>";  
												  }  
				  						     $("#onSelect").append(info);
									    	}
									    });

				  				});
				  				</script>


你可能感兴趣的:(jquery,Ajax,Ajax,springMVC,下拉列表动态加载)