繁体   English   中英

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

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

样本休息服务如下:

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

}

而且我写了这样的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));  
        }
       });

这个ajax代码正确吗?

不可以,因为ajax请求将不会传输文件数据,所以它将不起作用。

解决方案是

  1. 使用文件上传插件,例如jquery-form

例如:

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

    },
    error : function(response) {

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

暂无
暂无

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

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