簡體   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