jsonp跨域请求问题

$.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值