jsonp 例子
1. 返回结果必须是 text/javascript 类型
2.返回类型是String,不能包在其他对象里面
3.需要如同下面的jsonTest一样,在返回内容前面加上前端获取的参数,约定一下,参数名为“callback”
前端:
$.ajax({ type: "get", async: false, url: 'http://172.16.17.64:8089/hiap/jsonpTest', dataType: "jsonp", contentType: "text/javascript", jsonp: "callback",/*后台获取的参数名*/ jsonpCallback:"jsonpCallback",/*前端回调函数*/ success: function(json){ console.log("done"); console.log(json); }, error: function(e){ console.log("fail"); console.log(e); //window.location.reload(); } });
后台:
@RequestMapping(value="/jsonpTest",method=RequestMethod.GET,produces="text/javascript") @ResponseBody public String jsonpTest(Model model,HttpServletRequest httpRequest){ String callback = httpRequest.getParameter("callback"); callback=callback+"([ { name:'John',age:'19'},{ name:'joe',age:'20'}] );"; return callback; }
另外可以参考以下内容:
相关推荐
somebodyoneday 2020-04-22
adonislu 2019-07-01
fengchao000 2020-06-17
adonislu 2020-05-16
zmosquito 2020-05-10
adonislu 2020-05-10
fengchao000 2020-04-22
fengchao000 2020-04-11
Richardxx 2020-03-07
somebodyoneday 2020-03-06
fengchao000 2020-03-05
somebodyoneday 2020-02-16
xiaouncle 2020-02-13
baijinswpu 2020-01-29
fengchao000 2020-01-10
fengchao000 2019-12-25
newthon 2019-12-23