为什么不't我的angular submit函数添加了空数据

Why doesn't my angular submit function adding empty data

本文关键字:添加 函数 数据 submit angular 我的 为什么不      更新时间:2023-09-26

这是我第一次使用angular对REST APU进行POST方法调用。这是我的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;
    });
};

alert("add new type [" + $scope.newtype + "]" );按预期执行,所以我知道$scope.newtype有一个值。

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

我认为我没有正确设置data。有人能指出我做错了什么吗?

我不得不这么做。。。

 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     };