2016-09-08 105 views
0

在我的JS,我有這樣的:

... 
     var sourcesToCall = _.compact(sources); 
     var jsonfile= {json:JSON.stringify(sourcesToCall)}; 
     $.ajax({ 
      type:'POST', 
      url: "/info-web/downloadCSV", 
      data: jsonfile, 
      dataType: "json" 
     }); 
... 

我的控制器

@Controller 
@RequestMapping(value="/downloadCSV") 
public class DownloadCSVController { 
    @RequestMapping(method = RequestMethod.GET) 
    public void downloadCSV(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException{ 
     System.out.println("DEBUT TELECHARGEMENT"); 
     response.setContentType("text/csv"); 
     String reportName = "CSV_Report_Name.csv"; 
     response.setHeader("Content-disposition", "attachment;filename="+reportName); 

     ArrayList<String> rows = new ArrayList<String>(); 
     rows.add("Name,Result"); 
     rows.add("\n"); 

     for (int i = 0; i < 10; i++) { 
      rows.add("Java Honk,Success"); 
      rows.add("\n"); 
     } 

     Iterator<String> iter = rows.iterator(); 
     while (iter.hasNext()) { 
      String outputString = (String) iter.next(); 
      response.getOutputStream().print(outputString); 
     } 

     response.getOutputStream().flush(); 

     System.out.println("FIN TELECHARGEMENT"); 

    } 
} 

我有一個CorsFilter類:

@Component 
@Order(Ordered.HIGHEST_PRECEDENCE) 
public class SimpleCORSFilter implements Filter { 
    private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class); 

    public SimpleCORSFilter() { 
     log.info("SimpleCORSFilter init"); 
    } 

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
      throws IOException, ServletException { 

     HttpServletRequest request = (HttpServletRequest) req; 
     HttpServletResponse response = (HttpServletResponse) res; 

     response.setHeader("Access-Control-Allow-Origin", "*");// request.getHeader("Origin") 
     response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE"); 
     response.setHeader("Access-Control-Max-Age", "3600"); 
     response.setHeader("Access-Control-Allow-Headers", 
       "Origin, X-Requested-With, X-Auth-Token, Content-Type, Accept"); 
     // response.addHeader("Access-Control-Expose-Headers", 
     // "X-Requested-With, X-Auth-Token"); 
     if (request.getMethod() != "OPTIONS") { 
      chain.doFilter(req, res); 
     } else { 
      response.setStatus(HttpServletResponse.SC_OK); 
     } 
    } 

    public void init(FilterConfig filterConfig) { 
    } 

    public void destroy() { 
    } 
} 

在我的web.xml:

<filter> 
    <filter-name>CORSFilter</filter-name> 
    <filter-class>support.utils.SimpleCORSFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>CORSFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

我的錯誤:

POST http://localhost:8080/info-web/downloadCSV.json 405 (method not allowed)

+1

那麼,只有當請求的類型爲GET,但是您正在進行ajax POST調用時,您纔有動作'downloadCSV'。嘗試將此@RequestMapping(method = RequestMethod.GET)更改爲此@RequestMapping(method = RequestMethod.POST)' – reillyethan

回答

2

正如我在評論說,你有你的行動downloadCSV只如果請求的類型是GET被解僱,但你讓一個Ajax POST調用。試着將這個@RequestMapping(method = RequestMethod.GET)改爲這個@RequestMapping(method = RequestMethod.POST)

1

您還沒有實現POST方法在控制器上,寫筆記這條線路並且寫它爲崗位電話

@RequestMapping(method = RequestMethod.GET) 

我希望將工作