2015-07-12 57 views
2

我想從我的客戶端(AngularJs)向我的NodeJs服務器發送一個base64字符串數組。 我遇到了一個奇怪的情況。每當我送的物體,像這樣:Angular JS向BaseJs服務器發送base64陣列 - Allow-Access-Control-Origin

{Price: 1000000, LookingFor: "Rent", RoomsNumber: 4, Area: 1000, PropertyType: "Any", Base64:['data:image/jpeg;base64,/9..'] } 

我得到一個錯誤這樣的:

XMLHttpRequest cannot load http://localhost:8080/estate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 413. 

當我刪除的base64 HTTP請求正在工作。另外,當我只向服務器發送一個base 64字符串時,服務器接受它。

在客戶端,我採用了棱角分明的$ HTTP:

self.PostEstate = function (estate) { 
      console.log(serverLocation + 'estate'); 
      console.log('sending estate', estate); 
      $http.post(serverLocation + 'estate', estate) 
       .success(function (data) { 
        console.log('successfully sent estate', data); 
        $rootScope.$broadcast('postEstate', { 
         IsSucceded: true, 
         _id: data 
        }) 
       }).error(function (err) { 
        $rootScope.$broadcast('postEstate', { 
         Msg: err, 
         IsSucceded: false 
        }) 
       }); 
     } 

而且在我的服務器我使用我的應用程序的簡單的路由:

app.post('/estate', function (req, res) { 

    var estate = new Estate({ 
     Price: req.body.Price, 
     LookingFor: req.body.LookingFor, 
     RoomsNumber: req.body.RoomsNumber, 
     Area: req.body.Area, 
     PropertyType: req.body.PropertyType, 
     Desc: req.body.Desc, 
     photos: [] 
    }); 

    //Thats mongoose obj 
    estate.save(function (err) { 
     if (err) 
      res.send(err); 

     res.json({ 
      _id: estate._id 
     }); 
    }); 
}); 

我想,發佈請求太大,我可以刪除限制嗎? 如果不是,你能建議另一個架構,我可以發送base64陣列到我的服務器? 預先感謝您。 PS只是提到他我使用節點10與快遞4.

+0

是否有任何理由你不使用'多/形式 - 數據'上傳圖片? – m90

回答

1

你的服務器會告訴你什麼是錯的:

的反應有其轉化爲Request Entity too large

HTTP狀態碼413

看起來你想要上傳圖片,爲什麼你不簡單地使用multipart/form-data這樣做?你真的必須發送一個base64編碼的字符串嗎?

請參見本教程例如:https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs

如果你真的需要發送一個字符串,你可以增加限制在body-parser中間件:Node.js Express. Large body for bodyParser

+0

好吧,我不知道有上傳整個圖像的選項。我如何在服務器中處理這個問題? – Daffa

+0

這裏有一些中間件:https://www.npmjs.com/package/connect-multiparty#readme或https://www.npmjs.com/package/multer#readme來處理。 – m90