用jsonp解决easyui 跨域问题

服务端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\",{\"email\":\"[email protected]\",\"firstname\":\"fname3\",\"id\":5,\"lastname\":\"lname3\",{\"email\":\"[email protected]\",\"firstname\":\"fname4\",\"id\":7,\"lastname\":\"lname4\",{\"email\":\"[email protected]\",\"firstname\":\"fname5\",\"id\":8,\"lastname\":\"lname5\",{\"email\":\"[email protected]\",\"firstname\":\"fname6\",\"id\":9,\"lastname\":\"lname6\",{\"email\":\"[email protected]\",\"firstname\":\"fname7\",\"id\":10,\"lastname\":\"lname7\",{\"email\":\"[email protected]\",\"firstname\":\"fname8\",\"id\":11,\"lastname\":\"lname8\",{\"email\":\"[email protected]\",\"firstname\":\"fname9\",\"id\":12,\"lastname\":\"lname9\",{\"email\":\"[email protected]\",\"firstname\":\"fname10\",\"id\":13,\"lastname\":\"lname10\",\"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"  为跨域调用
<table id="dg1" title="用户管理" class="easyui-datagrid" style="width:100%;height:100%px"
			url="http://localhost:8888/study/test"  
			toolbar="#toolbar" pagination="true"
			rownumbers="true" fitColumns="true" singleSelect="true">
		<thead>
			<tr>
				<th field="firstname" width="50">姓</th>
				<th field="lastname" width="50">名</th>
				<th field="phone" width="50">电话</th>
				<th field="email" width="50">Email</th>
				<th field="classno" width="50">班级</th>
				
			</tr>
		</thead>
	</table>



js代码片段:

在jquery.easyui.min.js中增加一个跨域调用的js函数

$.ajax({type:opts.method,url:opts.url,data:_7ff,dataType:"jsonp",success:function(data){

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...