用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\",\"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"  爲跨域調用

<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){


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章