简体   繁体   English

这是ajax要求的休息服务的正确与否吗?

[英]is this ajax call for below rest Service is correct or not?

Sample rest Service is below: 样本休息服务如下:

@RequestMapping(value = "/image/upload", method = RequestMethod.POST)
public void uploadImage(@RequestParam("image") MultipartFile fileObj)
        throws Exception 
{
  System.out.print("File Name:"+fileObj.getOriginalFileName()); 

}

and i wrote ajax code like this : and my accept application format is Json when i call this i get 400 error 而且我写了这样的ajax代码:当我调用此代码时,我的接受应用程序格式为Json,我得到400错误

            $('#user_click').click(function(){
    var data = { 
              image:$("#file_1").val
                };
    $.ajax({
        url : "http://localhost:8080/MyProject/image/upload",
        type : "POST",
        contentType : false,
        crossDomain : true,
        data : JSON.stringify(data),
        dataType : 'json',
        async : true,
        success : function(result) {
            alert('The Selected Items uploaded');
        },
        error: function(message){
          alert("Error:"+JSON.stringify(message));  
        }
       });

is this ajax code is correct or not? 这个ajax代码正确吗?

No, it will not work since ajax request will not transfer file data. 不可以,因为ajax请求将不会传输文件数据,所以它将不起作用。

The solutions are 解决方案是

  1. Use a file upload plugin like jquery-form 使用文件上传插件,例如jquery-form

Ex: 例如:

$('#myForm').ajaxSubmit({
    url : 'http://localhost:8080/MyProject/image/upload',
    type : "POST",
    dataType : "json",
    success : function(response) {

    },
    error : function(response) {

    }
});
  1. Use html5 FormData (Sadly no IE support) 使用html5 FormData (很遗憾,没有IE支持)

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

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