$.ajax({
url: 'http://192.168.3.49:8080/PORTAL/authCode',
type: 'GET',
dataType:'jsonp',
jsonp: "callback",
success:function(data){
alert(data.ret)
},
error:function(data){
$('#mstr_ck').html("获取验证码失败,请重试!");
$("#error_ck").show();
}
});
2,java代码
@RequestMapping(value = "authCode")
@ResponseBody
public String getMobileAuthCode( HttpServletRequest request, String callback)
throws Exception {
String result = "{'ret':'true'}";
//加上返回参数
result=callback+"("+result+")";
return result;
}
直接这么写js返回永远进入error回掉方法中,错误提示为errorparser method was not called
正确执行的代码为:
@RequestMapping(value = "authCode")
@ResponseBody
public void getMobileAuthCode( HttpServletRequest request, HttpServletResponse response, String callback)
throws Exception {
String result = "{'ret':'true'}";
//加上返回参数
result=callback+"("+result+")";
response.getWriter().write(result);
response.flushBuffer()
}
这里需要思考的是第一种方法虽然返回的也是 callback({"ret":"true"}),但它返回到客户端时是字符串,
需要考虑@ResponseBody return String 和
@ResponseBody void response.getWriter().write(str) 的区别
另外关于jsonp的实现原理可参照下面的链接
http://www.qdfuns.com/notes/16738/1b6ad6125747d28592a53a960b44c6f4.html
http://www.360doc.com/content/14/1030/09/5054188_421070813.shtml
http://blog.csdn.net/u011897301/article/details/52679486
这里需要思考的是第一种方法虽然返回的也是 callback({"ret":"true"}),但它返回到客户端时是字符串,
需要考虑@ResponseBody return String 和 @ResponseBody void response.getWriter().write(str) 的区别
另外关于jsonp的实现原理可参照下面的链接
http://www.qdfuns.com/notes/16738/1b6ad6125747d28592a53a960b44c6f4.html
http://www.360doc.com/content/14/1030/09/5054188_421070813.shtml
http://blog.csdn.net/u011897301/article/details/52679486