解决ajax跨域问题
2015-12-02 15:26
405 查看
前台:
$.ajax({
type:'get',
url:urly+'url?iType=zxxx&callback=?',
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processData: false,
success:function(data){
for(var i=0; i<data.content.length; i++){
var tab = "<tr><td></td><td><a href='"+urly+"/gmcloud/interf/getZx.haze?
iId="+data.content[i].iId+"' target='_Blank' style='text-
decoration: none;'>"+data.content[i].iTli.substring(0,19)+"</a></td>
</tr>";
$("#xx").append(tab);
}
},
error: function(err) {
// alert("获取失败。");
}
});
后台(注意最后需要用callback在最外层包一下):
@RequestMapping("getByMh")
public void getByMh( HttpServletResponse response,HttpServletRequest request,InformationBean information) throws Exception{
response.setContentType("text/html;charset=UTF-8");
String msg = "";
int code = 0;
Map map = new HashMap();
String callback = request.getParameter("callback");
information.setiType(request.getParameter("iType"));
List<InformationBean> list = informationService.getByMh(information);
if(0!=list.size()&&!"".equals(list)){
map.put("content", list);
code=0;
msg="获取信息成功!";
}else{
map.put("content", "");
code=40029;
msg="无";
}try{
}catch(Exception e){
code=40029;
msg="获取信息失败";
}
map.put("code", code);
map.put("msg", msg);
PrintWriter out = response.getWriter();
JSONObject jsonObject = JSONObject.fromObject(map);
out.print(callback + "(" + jsonObject + ")");
}
$.ajax({
type:'get',
url:urly+'url?iType=zxxx&callback=?',
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processData: false,
success:function(data){
for(var i=0; i<data.content.length; i++){
var tab = "<tr><td></td><td><a href='"+urly+"/gmcloud/interf/getZx.haze?
iId="+data.content[i].iId+"' target='_Blank' style='text-
decoration: none;'>"+data.content[i].iTli.substring(0,19)+"</a></td>
</tr>";
$("#xx").append(tab);
}
},
error: function(err) {
// alert("获取失败。");
}
});
后台(注意最后需要用callback在最外层包一下):
@RequestMapping("getByMh")
public void getByMh( HttpServletResponse response,HttpServletRequest request,InformationBean information) throws Exception{
response.setContentType("text/html;charset=UTF-8");
String msg = "";
int code = 0;
Map map = new HashMap();
String callback = request.getParameter("callback");
information.setiType(request.getParameter("iType"));
List<InformationBean> list = informationService.getByMh(information);
if(0!=list.size()&&!"".equals(list)){
map.put("content", list);
code=0;
msg="获取信息成功!";
}else{
map.put("content", "");
code=40029;
msg="无";
}try{
}catch(Exception e){
code=40029;
msg="获取信息失败";
}
map.put("code", code);
map.put("msg", msg);
PrintWriter out = response.getWriter();
JSONObject jsonObject = JSONObject.fromObject(map);
out.print(callback + "(" + jsonObject + ")");
}
相关文章推荐
- 肯特·贝克:改变人生的代码整理魔法
- 你应该学习哪种编程语言?
- [转]我们需要一种其他人能使用的编程语言
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- DB2编程序技巧(1)
- DB2编程序技巧 (四)
- 女人VS编程_国庆快乐
- DB2编程序技巧 (六)
- DB2编程序技巧 (三)
- DB2编程序技巧 (九)
- DB2编程序技巧 (七)
- DB2编程序小小技巧
- DB2编程序技巧 (五)
- DB2编程序技巧 (一)
- DB2编程序技巧 (八)
- DB2编程序技巧 (十)
- 再谈Jquery Ajax方法传递到action(补充)
- VBS基础编程教程 (第1篇)
- VBS基础编程教程 (第3篇)
- Dom在ajax技术中的作用说明