简体   繁体   中英

JS $http.POST method 405 error

In my JS i have this:

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

My Controller :

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

    }
}

I have a CorsFilter class:

@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() {
    }
}

In my 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>

My error:

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

As I said in comment, you have your action downloadCSV to be fired only if the request's type is GET, but you're making an ajax POST call. Try to change this @RequestMapping(method = RequestMethod.GET) to this @RequestMapping(method = RequestMethod.POST)

You have not implemented POST method on the controller, take a note of this line and write it for post call

@RequestMapping(method = RequestMethod.GET)

I hope that would work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM