2017-06-14 129 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对象。我怀疑这不被支持,因此你的请求不能被序列化。 –