JS $ http.POST方法405错误

时间:2016-09-08 10:16:04

标签: javascript java spring cors

在我的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(方法   不允许)

2 个答案:

答案 0 :(得分:2)

正如我在评论中所说的那样,只有在请求的类型为GET但你正在进行ajax POST调用时才会触发你的动作downloadCSV。尝试将此@RequestMapping(method = RequestMethod.GET)更改为此@RequestMapping(method = RequestMethod.POST)

答案 1 :(得分:1)

您尚未在控制器上实现POST方法,请记下此行并将其写入后期调用

@RequestMapping(method = RequestMethod.GET)

我希望这会起作用