RestTemplate的exchange方法,post请求实现跨域操作

问题描述:

              前台页面访问第三方接口,http的post请求,实现跨域。浏览器控制台报错如下:

RestTemplate的exchange方法,post请求实现跨域操作

 

处理方法:

(1)前台js:

   RestTemplate的exchange方法,post请求实现跨域操作

(2)contoller处理:

   RestTemplate的exchange方法,post请求实现跨域操作

代码详情:

@RequestMapping(value = "/sjtb", method = RequestMethod.POST)
     @ResponseBody
    public JsonObject sjtb(String ywlsh,String ywfldm,HttpServletRequest req,HttpSession session) throws Exception {
        // TODO Auto-generated method stub
        ResponseEntity<String> forEntity = null;
        JsonObject json = null;


      RestTemplate restTemplate = new RestTemplate();
      // 准备调接口
      HttpHeaders headers = new HttpHeaders();
      String ip=  "http://"+Utils.getProperties("easip")+":"+Utils.getProperties("easport");
      // 将加密后的参数和请求头Headers封装到请求实体
      org.springframework.http.HttpEntity<String> httpEntity = new org.springframework.http.HttpEntity<>(headers);
      forEntity = restTemplate.exchange(ip+"/RSMemoryAction.do?action=AjaxAction&ajaxactioncode=CheckUpDataAction&vailduserlogin=false&field01="+ywlsh+"&business_code="+ywfldm+"", HttpMethod.POST, httpEntity , String.class);
      System.err.println("档案数据转移结果>>>>>>>>>>>>>>>="+forEntity.toString());
      return json;
    }