由于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决ajax的跨域问题。本篇将讲述一个小白从遇到跨域不知道是跨域问题,到知道是跨域问题不知道如何解决,再到解决跨域问题,最后找到两种方法解决ajax跨域问题的全过程。
不知是跨域问题
起因是这样的,为了复用,减少重复开发,单独开发了一个用户权限管理系统,共其他系统获取认证与授权信息,暂且称之为A系统;调用A系统以B为例。在B系统中用ajax调用A系统系统的接口(数据格式为json),当时特别困惑,在A系统中访问相应的url可正常回返json数据,但是在B系统中使用ajax请求同样的url则一点儿反应都没有,好像什么都没有发生一样。这样反反复复改来改去好久都没能解决,于是求救同事,提醒可能是ajax跨域问题,于是就将这个问题当做跨域问题来解决了。
知跨域而不知如何解决
知道问题的确切原因,剩下的就是找到解决问题的方法了。google了好久,再次在同事的指点下知道jQuery的ajax有jsonp这样的属性可以用来解决跨域的问题。
现在也知道了怎样来解决跨域问题,余下的就是实现的细节了。实现的过程中错误还是避免不了的。由于不了解json和jsonp两种格式的区别,也犯了错误,google了好久才解决。
首先来看看在页面中如何使用jQuery的ajax解决跨域问题的简单版:
01 |
$(document).ready( function (){ |
02 |
var url='http: //localhost:8080/WorkGroupManagment/open/getGroupById" |
04
$.ajax({ |
06
dataType:'jsonp' , |
07 |
processData:false 08 |
type:'get' 09 |
success:(data){ |
10
alert(data.name); |
11 |
},monospace!important; white-space:Nowrap; float:none!important; height:auto!important; font-size:10pt!important; vertical-align:baseline!important; border-top:0px; right:auto!important; border-right:0px; padding-top:0px!important; top:auto!important; left:auto!important">12 |
error:(XMLHttpRequest,textStatus,errorThrown) { |
13 |
alert(XMLHttpRequest.status); |
14
alert(XMLHttpRequest.readyState); |
16
}}); |
这样写是完全没有问题的,起先error的处理函数中仅仅是alert(“error”),为了进一步弄清楚是什么原因造成了错误,故将处理函数变为上面的实现方式。最后一行alert使用为;parsererror。百思不得其解,继续google,最终还是在万能的stackoverflow找到了答案,链接在这里。原因是jsonp的格式与json格式有着细微的差别,所以在server端的代码上稍稍有所不同。
比较一下json与jsonp格式的区别:
json格式:
2 |
"message": "获取成功" ottom:0px; position:static!important; border-left:0px; padding-bottom:0px!important; line-height:1.1em!important; margin:0px; outline-style:none!important; outline-color:invert!important; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:Consolas, |
3 |
"state""1" ottom:0px; position:static!important; border-left:0px; padding-bottom:0px!important; line-height:1.1em!important; margin:0px; outline-style:none!important; outline-color:invert!important; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:Consolas, |
4
"result":{ "name" "工作组1" ottom:0px; position:static!important; border-left:0px; padding-bottom:0px!important; line-height:1.1em!important; margin:0px; outline-style:none!important; outline-color:invert!important; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:Consolas, "id" :1,monospace!important; white-space:Nowrap; float:none!important; height:auto!important; color:blue!important; font-size:10pt!important; vertical-align:baseline!important; border-top:0px; right:auto!important; border-right:0px; padding-top:0px!important; top:auto!important; left:auto!important">"description" "11" } |
jsonp格式:
看出来区别了吧,在url中callback传到后台的参数是神马callback就是神马,jsonp比json外面有多了一层,callback()。这样就知道怎么处理它了。于是修改后台代码。
后台java代码最终如下:
01
@RequestMapping(value = "/getGroupById" ) |
02
publicString getGroupById( @RequestParam ( ) Long id,monospace!important; white-space:Nowrap; float:none!important; height:auto!important; font-size:10pt!important; vertical-align:baseline!important; border-top:0px; right:auto!important; border-right:0px; padding-top:0px!important; top:auto!important; left:auto!important">03 |
HttpServletRequest request,HttpServletResponse response) |
04
throwsIOException { |
05 |
String callback = request.getParameter("callback" ); |
06
ReturnObject result =null ; |
08
try{ |
09 |
group = groupService.getGroupById(id); |
10
result =new ReturnObject(group,Constants.RESULT_SUCCESS); |
11 |
}catch (BusinessException e) { |
13 |
"获取失败"ottom:0px; position:static!important; border-left:0px; padding-bottom:0px!important; line-height:1.1em!important; margin:0px; outline-style:none!important; outline-color:invert!important; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:Consolas,Constants.RESULT_Failed); |
14
15 |
String json = JsonConverter.bean2Json(result); |
16
response.setContentType("text/html" ); |
17 |
response.setCharacterEncoding("utf-8" 18 |
PrintWriter out = response.getWriter(); |
19 |
out.print(callback +"(" + json + ")" 20 |
return21 |
} |