标签:
页面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();
}
}
标签:
原文地址:http://www.cnblogs.com/qlong8807/p/5377447.html