简体   繁体   English

为什么我的角度提交功能不添加空数据

[英]Why doesn't my angular submit function adding empty data

This is my first time using angular to make POST method calls to my REST APU. 这是我第一次使用angular对我的REST APU进行POST方法调用。 This is the function in my typeCtrl.js file: 这是我的typeCtrl.js文件中的函数:

$scope.submit  = function() {
    alert("add new type [" + $scope.newtype + "]" );
    $http.post("http://192.168.1.115:8080/type/add", { 'type': $scope.newtype}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
        console.log(response);
        console.log(response.data);
    });
    $http.get("http://192.168.1.115:8080/type").then(function(response) {
        console.log(response);
        console.log(response.data);
        $scope.all_types = response.data;
    });
};

The alert("add new type [" + $scope.newtype + "]" ); alert("add new type [" + $scope.newtype + "]" ); executes as expected so I know that $scope.newtype has a value. 按预期执行,所以我知道$scope.newtype有一个值。

The output from my node.js backend looks like this: 我的node.js后端的输出如下所示:

POST /type/add 200 2.267 ms - 73
GET /type 200 23.854 ms - 6465
GET /type 304 22.217 ms - -
undefined
{ '{"type":"aaaa"}': '' }
POST /type/add 200 1.863 ms - 73
GET /type 200 26.053 ms - 6508
undefined
{ '{"type":"zzzzzzz"}': '' }
POST /type/add 200 1.734 ms - 73
GET /type 200 25.389 ms - 6551
undefined
{ '{"type":"zzzzzzz"}': '' }
POST /type/add 200 2.142 ms - 73
GET /type 200 25.435 ms - 6594

I do not think I am setting up my data correctly. 我认为我没有正确设置data Can anyone please point out what I am doing wrong? 谁能指出我做错了吗?

I had to do this ... 我必须这样做...

 33     $scope.submit  = function() {
 34         $scope.formdata = "{'type':'"+$scope.newtype+"'}";
 35         alert("add new type [" + $scope.newtype + "]" );
 36         var data = $.param({ type: $scope.newtype });
 37         $http.post("http://192.168.1.115:8080/type/add",
 38             // [{'type': $scope.newtype}],
 39             data,
 40             {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
 41             console.log(response);
 42             console.log(response.data);
 43         });
 44         $http.get("http://192.168.1.115:8080/type").then(function(response) {
 45             console.log(response);
 46             console.log(response.data);
 47             $scope.all_types = response.data;
 48         });
 49     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM