2015-07-03 13 views
0

我與此代碼得到的錯誤是這個REST API代碼有什麼問題?

不能發佈/ IMG /上傳/」

代碼:

app.post('/img/upload/',[multer({ dest: __dirname+'/www/images/new/', 
      rename: function (fieldname, filename, req, res) { 
        return filename + '_ORIGINAL'; 
      }, 
      onFileUploadStart: function (file, req, res) { 
        console.log(file.fieldname + ' is starting ...') 
      }, 
      onFileUploadData: function (file, data, req, res) { 
        console.log(data.length + ' of ' + file.fieldname + ' arrived') 
      }, 
      onFileUploadComplete: function (file, req, res) { 
        console.log(file.fieldname + ' uploaded to ' + file.path) 
      }, 
      onError: function (error, next) { 
        console.log(error) 
        next(error) 
      } 
    })]); 

控制器請求這個API是:

var data = new FormData; 
    data.append("file", files[0]); 

    $http({ 
      url: 'http://52.25.181.109/img/upload/', 
      method: "POST", 
      data : data, 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     }).success(function (data, status, headers, config) { 
      $scope.hide_u(); 
      console.log(data); 
     }).error(function (data, status, headers, config) { 
      $scope.hide_u(); 
      console.log(data); 
     }); 

代碼執行錯誤。我該如何糾正?

+0

您是否申請不同的域名? – binariedMe

+0

是所有的錯誤信息說?或者有什麼關於跨域的呢? @RohitKumar建議似乎更可能,因爲您在該網址中使用IP地址。 – NikhilWanpal

+0

這是一個發佈的請求,我正在aws數據庫。其他GET請求工作正常。我遇到問題只爲這個POST請求。 –

回答

0

嘗試更改標題:headers: {'Content-Type': 'multipart/form-data'} Multer只能處理多部分請求。該路線對於「application/x-www-form-urlencoded」無效。形成他們的網站:

Node.js處理中間件multipart/form-data

更新: 我建立了一個服務器。以下應該工作,更改服務器處理器:

app.use(multer({ dest: __dirname+'/www/images/new/'})); 
app.post("/img/upload/", function() {console.log("request rec.")}); 

我的測試形式爲:"<form enctype=\"multipart/form-data\">"

建議:嘗試刪除頭。

+0

添加標題,它要求提供邊界。我指定了邊界並且仍然存在同樣的問題。 –

+0

@AbhayNaik檢查更新的答案。 – NikhilWanpal