服务端servlet代码片段:
/**
* 处理请求
* @throws IOException
*/
private void requestManage(HttpServletRequest req, HttpServletResponse res) throws IOException{
//配置编码类型
res.setContentType("text/html;charset=UTF-8;pageEncoding=UTF-8"); //控制浏览器的编码行为
res.setCharacterEncoding("UTF-8");//目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;
res.setHeader("content-type","text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");
String jason="{\"currentNumber\":10,\"pageNo\":1,\"pageSize\":10,\"rows\":[{\"email\":\"[email protected]\",\"firstname\":\"fname1\",\"id\":3,\"lastname\":\"lname1\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname2\",\"id\":4,\"lastname\":\"lname2\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname3\",\"id\":5,\"lastname\":\"lname3\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname4\",\"id\":7,\"lastname\":\"lname4\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname5\",\"id\":8,\"lastname\":\"lname5\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname6\",\"id\":9,\"lastname\":\"lname6\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname7\",\"id\":10,\"lastname\":\"lname7\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname8\",\"id\":11,\"lastname\":\"lname8\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname9\",\"id\":12,\"lastname\":\"lname9\",\"phone\":\"(000)000-0000\",\"valid\":true},{\"email\":\"[email protected]\",\"firstname\":\"fname10\",\"id\":13,\"lastname\":\"lname10\",\"phone\":\"(000)000-0000\",\"valid\":true}],\"total\":11,\"totalPages\":2}";
String callback=req.getParameter("callback");//得到回调函数名
System.out.println(callback);
res.getWriter().write(callback+"("+jason+")");//封装成 回调函数名(data)形式
}
前端:
url="http://localhost:8888/study/test" 为跨域调用
姓
名
电话
Email
班级
js代码片段:
在jquery.easyui.min.js中增加一个跨域调用的js函数
$.ajax({type:opts.method,url:opts.url,data:_7ff,dataType:"jsonp",success:function(data){