Call the Ajax cross-origin call interface

Source: Internet
Author: User

tag: HTTP Io OS Ar data SP C on CTI

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

 

调用ajax 跨域调用接口

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.