jsonp 自己写的一个例子

function test(){
alert("13");
$.ajax({
type : "GET",
async:false,
url : "http://192.168.1.231:8084/lesiea/test?id=1&name=zfx",
dataType : "jsonp",//数据类型为jsonp
jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数
success : function(data){
alert(data.result);
//$("#showcontent").text("Result:"+data.result)
},
error:function(){
alert('fail');
}
});
}
服务器端:
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 = response.getWriter();
String s=new JsonObjectMapper().writeValueAsString(map);
String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数
out.println(jsonpCallback+"("+s+")");//返回jsonp格式数据
out.flush();
out.close();
 
posted @ 2016-07-22 16:57  呵呵|java  阅读(424)  评论(0编辑  收藏  举报