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")); } } }