2015-07-21 111 views
2

我有這樣的Ajax請求:我沒有收到來自FORMDATA數據在我sails.js服務器

this.sendApiRequestWithFile = function (method) { 
    var formData = new FormData(); 
    formData.append("name", "my name"); 

    data_ajax = { 
     url: "http://localhost:1337/" + method, 
     method: "PUT", 
     data: formData, 
     headers: { 
      'Cache-Control': 'no-cache', 
      'Content-Type': 'multipart/form-data; boundary=----', 
     } 
    } 

    return $http(data_ajax).success(function(data, status, headers, config) { 
     return data; 
    }).error(function(data, status, headers, config) { 
     return data; 
    }); 
} 

而我的服務器是在sails.js讓我趕上這樣的參數:req.body它不起作用。我嘗試req.params.all()並且不起作用。

+0

這是解決了。問題是我不得不使用jQuery來做一個Ajax。 – oihi08

回答

1

我希望下面的代碼應該可以工作。如果您嘗試訪問服務器從上傳的文件,使用req.file( 「FILE_NAME」) 變種FD =新FORMDATA() fd.append( 「名」, 「名值」)

$.ajax({ 
     url: "/url", 
     type: "POST", 
     data: fd, 
     processData: false, 
     contentType: false, 
     success: function(response) { 
      console.log("Success : " + response); 
     }, 
     error: function(jqXHR, textStatus, errorMessage) { 
      console.log(errorMessage); // Optional 
     } 
    }); 
    }); 
相關問題