$.ajax({ type: "POST", url: "http://server/person/test", data: {}, dataType: "jsonp", jsonpCallback:"abcd", success: function (data) { alert(data.msg) }, error: function (XMLHttpRequest, textStatus, errorThrown) { } }); $.ajax({ type: "POST", url: "http://sw.xiaoyutest.cn/person/test2?callback=?", data: {}, dataType: "jsonp", success: function (data) { alert(data.msg) }, error: function (XMLHttpRequest, textStatus, errorThrown) { } }); $http.jsonp("http://server/person/test2?callback=JSON_CALLBACK").success( function(data){ $scope.info = data.info; } ).error( function(data){ alert("error"); } ); $.ajax({ url:"http://server/person/test", type:"post", dataType:"jsonp", jsonpCallback:"abcd", success:function(data){ /*console.log(data)*/ } }) $.ajax({ url:"http://server/person/test4", type:"get", success:function(data){ console.log(data) var aa = eval("("+data+")"); } })
@RequestMapping("/test") @ResponseBody public String Test(HttpServletRequest request){ String rt = "abcd({ msg:‘this is json‘,info:[{name:‘xiaoming‘,sex:‘f‘},{name:‘xiaohong‘,sex:‘m‘}]})"; return rt; } @RequestMapping("/test2") @ResponseBody public String Test2(HttpServletRequest request){ String name = request.getParameter("callback"); String rt = name+"({ msg:‘this is json‘,info:[{name:‘xiaoming‘,sex:‘f‘},{name:‘xiaohong‘,sex:‘m‘}]})"; return rt; } @RequestMapping("/test3") @ResponseBody public String Test3(HttpServletRequest request){ String name = request.getParameter("callback1"); String rt = name+"({ msg:‘this is json‘,info:[{name:‘xiaoming‘,sex:‘f‘},{name:‘xiaohong‘,sex:‘m‘}]})"; return rt; } @RequestMapping("/test4") @ResponseBody public String Test4(HttpServletRequest request,HttpServletResponse response){ response.addHeader("Access-Control-Allow-Origin","*"); //String rt = "{msg:‘this is json‘,info:[{name:‘xiaoming‘,sex:‘f‘},{name:‘xiaohong‘,sex:‘m‘}]}"; List list = new ArrayList(); list.add("xiaoming"); list.add("小明"); Result result = new Result(); result.setData(list); result.setMessage("成功"); return JSON.toJSONString(result); }
时间: 2024-10-14 14:18:00