下面rest服务的ajax调用是否正确?

下面rest服务的ajax调用是否正确?,ajax,spring,Ajax,Spring,rest服务示例如下: @RequestMapping(value = "/image/upload", method = RequestMethod.POST) public void uploadImage(@RequestParam("image") MultipartFile fileObj) throws Exception { System.out.print("File Name:"+fileObj.getOriginalFileName()); } 我

rest服务示例如下:

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

}
我编写了如下ajax代码: 我的accept应用程序格式是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));  
        }
       });

此ajax代码是否正确?

否,它将不起作用,因为ajax请求不会传输文件数据

解决办法是

  • 使用文件上传插件,如
  • 例:

  • 使用html5(遗憾的是没有IE支持)
  • $('#myForm').ajaxSubmit({
        url : 'http://localhost:8080/MyProject/image/upload',
        type : "POST",
        dataType : "json",
        success : function(response) {
    
        },
        error : function(response) {
    
        }
    });