简体   繁体   English

RequestParam批注不适用于POST

[英]RequestParam annotation doesn't work with POST

I have the following controller: 我有以下控制器:

@RequestMapping(value = { "/member/uploadExternalImage",
            "/member/uploadExternalImage" }, method = RequestMethod.POST)
    public String handleFileUpload(@RequestParam("url") String url,
            Principal principal) throws IOException {
        File file = restTemplate.getForObject("url", File.class);
        file.toString();
        return null;
    }

And I have the following ajax: 而且我有以下ajax:

      $.ajax({
            url: 'uploadExternalImage',  //Server script to process data
            type: 'POST',
            success: function () {

            },
            complete:function(){
                $.fancybox.hideLoading();
            },

            // Form data
            data: {url: files[0].link},

            cache: false,
            contentType: false,
            processData: false
       });

in spring log I see that 在春季日志中,我看到了

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'url' is not present

if I change ajax method with GET: 如果我用GET更改ajax方法:

$.ajax({
                url: 'uploadExternalImage?url=123',  //Server script to process data
                type: 'POST',
                success: function () {

                },
                complete:function(){
                    $.fancybox.hideLoading();
                },
                error: function (data) {

                },

                cache: false,
                contentType: false,
                processData: false
           });

it works fine 它工作正常

How does to configure spring correctly? 如何正确配置弹簧?

Http POST is capable of being used for request parameters on request body. Http POST可以用于请求正文上的请求参数。 But you are trying to use it on a different way. 但是您正在尝试以其他方式使用它。

Try this: 尝试这个:

            $.ajax({
                type: 'POST',
                url: "uploadExternalImage?url=BLABLA",
                data: text,
                success: function (data) {
                    //
                }
            });

If you want it on request body then use @RequestBody 如果要在请求正文中使用它,请使用@RequestBody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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