2017-06-14 49 views
2

我想提交信息給在線服務器,它要求我使用FormData發送信息。 所以我用下面的函數,該函數

var my_app = angular.module("my-app",[]) 
my_app.controller("MainController", function($scope, $http){ 
    $scope.master = {title: "Title",desc: "Desctibe", tags: ["One","Two"], category: ["1","2"] }; 
    var url = "http://freedoctor.southeastasia.cloudapp.azure.com/api/forum/create"; 
    var config = {"headers":{"Authorization":"JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU5MzEwNDUxZjk3MjJmZTQyODE1NTIxYSIsInJvbGUiOiJwYXRpZW50IiwiaWF0IjoxNDk3NDE4ODA3fQ.ODrSlYwxvrPb93IzHW3s-7NIVFsOL4pKzqZx8yMqWWE"}}; 

    $scope.booking = function(){ 
     $scope.title=$scope.user.title; 
     $scope.desc=$scope.user.desc; 
     $scope.tags=$scope.user.tags; 
     $scope.category=$scope.user.category; 
     $scope.askedTo=$scope.user.askedTo; 
     var info = new FormData(); 
     info.append("title", $scope.title); 
     info.append("desc", $scope.desc); 
     info.append("tags", $scope.tags); 
     info.append("category", $scope.category); 


     $http.post("http://localhost:8080/"+url, info, config).then(
     function(response){ 
      console.log(response); 
      $scope.reply = response.data; 
      }, 
      function(response){ 
       console.log(response); 
       $scope.reply = response.data; 
      }); 
    } 

    $scope.reset = function() { 
     $scope.user = angular.copy($scope.master); 
    }; 
    $scope.reset(); 
}); 

所以每當我撥打預訂功能,我得到一個內部服務器錯誤

SyntaxError: Unexpected token - 
at parse (/home/freedoctor/fdhbackend/node_modules/body-parser/lib/types/json.js:83:15) 
at /home/freedoctor/fdhbackend/node_modules/body-parser/lib/read.js:116:18 
at invokeCallback (/home/freedoctor/fdhbackend/node_modules/raw-body/index.js:262:16) 
at done (/home/freedoctor/fdhbackend/node_modules/raw-body/index.js:251:7) 
at IncomingMessage.onEnd (/home/freedoctor/fdhbackend/node_modules/raw-body/index.js:307:7) 
at emitNone (events.js:86:13) 
at IncomingMessage.emit (events.js:185:7) 
at endReadableNT (_stream_readable.js:974:12) 
at _combinedTickCallback (internal/process/next_tick.js:74:11) 
at process._tickDomainCallback (internal/process/next_tick.js:122:9) 

我知道FORMDATA工作,因爲我這個

檢查出來
for (var pair of info.entries()) { 
    console.log(pair[0]+ ', ' + pair[1]); 
} 

回答

3

您試圖沿着formData對象傳遞的其他數據應附加到data屬性中FormData對象。我曾經寫過以下功能。應爲你工作了:

this.uploadFile = function (url, dataToUpload) { 
      var data = new FormData(); 
      data.append("file", dataToUpload.file); //if you want to upload files along, else ignore this line 
      data.append("data", angular.toJson(dataToUpload.data)); //you can pass an object containing additional data and then append it here to the data property 
      return $http.post(url, data, { 
       transformRequest: angular.identity, 
       headers: { 'Content-Type': undefined } 
      }); 
     } 

你可以連續使用您的.then回調,從這個函數返回的對象。

如果您遇到任何問題,請嘗試此操作並進行更新。

+1

我回答了類似的問題https://stackoverflow.com/questions/44405200/unable-to-loop-through-string-array-and-append-to-formdata/44405713#44405713。可能是你可以以某種方式與它聯繫 –

+1

感謝您的回答,但我不認爲這將適用於我的情況。因爲我只想在請求中發送字符串。 我只是無法理解我撥打電話後收到的錯誤 –

+0

我看到您正在嘗試將自定義屬性添加到FormData對象。我懷疑這不被支持,因此你的請求不能被序列化。 –