首页 > 编程语言 > 详细

ajax跨域请求,页面和java服务端的写法

时间:2016-04-11 11:36:18      阅读:264      评论:0      收藏:0      [点我收藏+]

页面ajax请求的写法:

$.ajax({
		type : "get",
		async : false,
		cache : false,
		url : "http://localhost:8081/a/b",
		data : {
			produ_id: 111,
			sub_id: 0,
			produ_quantity: 1,
			produ_price: 0.0
		},
		dataType : "jsonp",
		jsonp: "jsonpCallback",
		success : function(data) {
			var d = data;
			alert(d);
		},
		error : function() {
			alert(‘fail‘);
		}
	});

  

java服务端写法:

public void ajaxRequest(Params params) {
		HttpServletRequest request = ;
		HttpServletResponse response = ;
		response.setContentType("text/plain");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "content");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String jsonString = JSONObject.toJSONString(map);//随便使用哪个JSONObject都可以,这里只是转为json格式的字符串就行
			String jsonpCallback = request.getParameter("jsonpCallback");// 客户端请求参数
			out.println(jsonpCallback + "(" + jsonString + ")");// 返回jsonp格式数据
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}

  

 

ajax跨域请求,页面和java服务端的写法

原文:http://www.cnblogs.com/qlong8807/p/5377447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!