//ajax 跨域请求数据
function ajaxType (){
$.ajax({
url: "http://127.0.0.1:9090/spring_mvc/HttpClient/ajaxType.do?jsonpCallback=?",
type: "GET",
data: { pwd: encodeURI('周'), username: 'tl' },
dataType: "jsonp",
success: function(data) {
//返回 为 map 一个 key 对应 多个 list
var list = data.ok ;
//获取长度list.length
alert(list[0]);//List第一个数据
}
});
}
控制器
//调用ajax 跨域调用接口
@RequestMapping(value = "ajaxType", method = RequestMethod.GET)
public void ajaxType(HttpServletRequest request, HttpServletResponse response){
try {
Map<String, List<Object>> map = new HashMap<String, List<Object>>();
List<Object> list = new ArrayList<Object>();
list.add("测试1");
list.add("测试2");
list.add("测试3");
list.add("测试4");
list.add("测试5");
map.put("ok", list);
String pwd = URLDecoder.decode(request.getParameter("pwd"),"UTF-8");//解码
PrintWriter out = response.getWriter();
JSONObject resultJSON = JSONObject.fromObject(map); // 根据需要拼装json
String jsonpCallback = request.getParameter("jsonpCallback");// 客户端请求参数
out.println(jsonpCallback + "(" + resultJSON.toString(1, 1) + ")");// 返回jsonp格式数据
System.out.println(jsonpCallback + "(" + resultJSON.toString(1, 1) + ")");
out.flush();
out.close();
} catch (Exception e) {
}
}