ajax跨域jsonp处理

前端之家收集整理的这篇文章主要介绍了ajax跨域jsonp处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

jquery 请求页面:

url=‘http://xx.xx.xx.xx:8080/form/remoteResource?jsonCallback=?’,这里的jsonCallback=?必不可少

$.ajax({
url: url,
type: "get",
data: param,
dataType: "jsonp",
success: function(d){
related.html(createOptions(d));
});


服务器端:

protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { String sourcetype = req.getParameter("sourceType");// log.info("remote sourceType is "+ sourcetype); String remoteCode = req.getParameter("remoteCode"); if(StringUtils.isNotBlank(remoteCode)) { ResourceService service = new ResourceServiceImpl(); String value = service.getResource(remoteCode); if(StringUtils.isNotBlank(value)) { String result = ""; String callback = req.getParameter("jsonCallback"); if(StringUtils.isBlank(callback)) { resp.setContentType("text/html;charset=utf-8"); result = value; }else { resp.setHeader("Access-Control-Allow-Origin","*"); resp.setDateHeader("Expires",0); resp.setContentType("text/javascript;charset=utf-8"); result = callback + "(" + value + ")"; } resp.getOutputStream().write(result.getBytes("UTF-8")); } } }

猜你在找的Ajax相关文章