Javascript 为什么';t my angular submit函数添加空数据
这是我第一次使用angular对REST APU进行POST方法调用。 这是我的typeCtrl.js文件中的函数:Javascript 为什么';t my angular submit函数添加空数据,javascript,angularjs,Javascript,Angularjs,这是我第一次使用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': '
$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;
});
};
警报(“添加新类型[+$scope.newtype+””)代码>按预期执行,因此我知道$scope.newtype
有一个值
my 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
我认为我的数据设置不正确。有人能指出我做错了什么吗?我不得不这么做
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 };