码上敲享录 > jquery入门实例分享 > 使用jsonp解决ajax跨域问题时返回json数据

使用jsonp解决ajax跨域问题时返回json数据

上一章章节目录下一章 2018-12-12已有1493人阅读 评论(0)

使用jsonp解决ajax跨域问题时返回json数据


解决方法:

1.前端代码

$.ajax({

  type: "POST",

  url: "/com/start.do",

  data:{bill_id:bill_id},

                  dataType:"jsonp",

  success: function(data){

  alert("返回json");

  }

});


2.后台代码:

@RequestMapping("/startFlow")

public void startFlow(HttpServletRequest request,HttpServletResponse response) throws Exception{

String bill_id = request.getParameter("bill_id");

Map<Object, Object> context = service.startFlow(bill_id);

String callback=request.getParameter("callback");

if(!StringUtil.isEmpty(callback)){

response.setContentType("application/json;charset=UTF-8");

try {

//设置页面不缓存

response.setHeader("Pragma", "No-cache");

response.setHeader("Cache-Control", "no-cache");

response.setCharacterEncoding("UTF-8");

PrintWriter out= null;

out = response.getWriter();

out.print(callback+"("+JSONUtil.toJSONString(context)+")");

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}


本文链接:http://www.yayihouse.com/yayishuwu/chapter/1745
向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交