方式: js前端请求: function getOcrInfo(imageData){ $.ajax({ url: 'http://localhost:8080/LSWS/ws/ocr/getWebImageRecognitionJsonStringByBase64Image', type: 'post', dataType:'json', //async:false, data: { "imageData" : imageData }, success:function(data){ if(data.flag==0){ var name=decodeURIComponent(data.name); var position=decodeURIComponent(data.position); }else{ FR.Msg.alert("错误提示","获取识别结果失败,请重试!"); } }, error:function(data){ FR.Msg.alert("错误提示","获取识别结果失败,请重试!"); } }); 服务器后端: @RequestMapping(value="/getWebImageRecognitionJsonStringByBase64Image", method = RequestMethod.POST) @ResponseBody public String getWebImageRecognitionJsonStringByBase64Image(HttpServletRequest request,HttpServletResponse response,String imageData) { String result = ""; //System.out.println(imageData); try{ WebImageRecognition gr = new WebImageRecognition(); result = gr.getWebImageRecognitionJsonObjectByBase64Image(imageData); response.addHeader("Access-Control-Allow-Origin", "*"); //用于ajax post跨域(*,最好指定确定的http等协议+ip+端口号) response.setCharacterEncoding("utf-8"); //response.getWriter().write(result); //response.getWriter().close(); }catch(Exception e){ e.printStackTrace(); logger.error("getGeneralRecognition:"+e); result = "{\"flag\":\"1\",\"errorMessage\":\"server change error at OcrController!\"}"; } return result; } 2、CORS 请求(可以发送跨域的post、put、get等请求) 在JSP或者Java代码中设置response的响应头 下面是是用过滤器简单的实现跨域请求放行 import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; public class TestFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse)servletResponse; response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Methods","GET,POST,PUT,OPTIONS"); response.setHeader("Access-Control-Allow-Credentials","true"); chain.doFilter(request, response); System.out.println(" **********************CROS 执行了 **********************"); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } 在web.xml中配置相应的拦截路径 <filter> <filter-name>webAppW</filter-name> <filter-class>com.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>webAppW</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --------------------- 作者:启示收藏 来源:CSDN 原文:https://blog.csdn.net/u012737182/article/details/62041890 版权声明:本文为博主原创文章,转载请附上博文链接!