加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax 结合struts实现跨域访问

发布时间:2020-12-16 02:05:48 所属栏目:百科 来源:网络整理
导读:1.首先ajax的访问方法如下: $.getJSON("requestUrl?callback=?",function(data){}); 2.struts中action 的写法如下: public String getBirthdayUser() {ListUser list = userService.listAll(1,100);Calendar cal = Calendar.getInstance();int month = cal

1.首先ajax的访问方法如下:

$.getJSON("requestUrl?callback=?",function(data){
	
});


2.struts中action 的写法如下:
public String getBirthdayUser() {
		List<User> list = userService.listAll(1,100);
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		List<User> brithdayUser = new ArrayList<User>();
		List<String> birthdayUserName = new ArrayList<String>();
		for (User user : list) {
			if (user.getIdCard() != null && user.getIdCard().length() == 18) {
				if (Integer.valueOf(user.getIdCard().substring(10,12)) == month
						&& Integer.valueOf(user.getIdCard().substring(12,14)) == day) {
					brithdayUser.add(user);
					birthdayUserName.add(user.getUserName());
				}
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		userJson.put("birthdayUser",birthdayUserName);
		return "getBirthdayUserSuccess";

	}

3.struts的配置文件如下:
<result name="getBirthdayUserSuccess" type="json">
				<param name="root">userJson</param>
				<param name="callbackParameter">callback</param>
			</result>


至此 ajax结合struts 实现跨域访问结束

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读