AngularJs http请求将我的数据连接成JSON密钥

时间:2017-05-10 02:19:52

标签: javascript angularjs json xmlhttprequest angularjs-http

有时行为是如此离奇,以至于我甚至不知道如何开始谷歌它。我是Angular的新手,我正在尝试从客户端向我的节点服务器发送POST数据。这是客户端的控制器:

var app = angular.module("groomer", []);
app.controller("gCtrl", function($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $scope.send = function() {
        $http({
            method : "POST",
            url : "/gUpdate",
            data: {
                gName:$scope.gName,
                gPhone:$scope.gPhone,
                gWebsite:$scope.gWebsite,
                gEmail:$scope.gEmail,
                gCustAcct:$scope.gCustAcct,
                gAddress:$scope.gAddress,
                gNotes:$scope.gNotes
            }
        }).then(function success(response) {
            alert(console.log('Success!'));
        }, function error(response) {
            alert(console.log('Booooooo'));
        });
    };
});

我天真想象应该出现在服务器上的是:

    req.body = {
        gName:'a', 
        gPhone:'b', 
        gWebsite:'c', 
        gEmail:'d', 
        gCustAcct:'e', 
        gAddress:'f', 
        gNotes:'g'
    }

但事情变得奇怪。实际上在服务器上显示的是请求正文:

{"{\"gName\":\"a\",\"gPhone\":\"b\",\"gWebsite\":\"c\",\"gEmail\":\"d\",\"gCustAcct\":\"e\",\"gAddress\":\"f\",\"gNotes\":\"g\"}":""}

如果你需要一秒钟来看看这里发生了什么,那就是我在对象中的所有JSON密钥和数据,双引号,转义,连接为字符串,并作为JSON传递给对象内的服务器对应于空值的键。

//so
var lifeMakesSense = {gName:'a',gPhone:'b',gWebsite:'c',gEmail:'d',gCustAcct:'e',gAddress:'f',gNotes:'g'}
//becomes
var waitNo = "{\"gName\":\"a\",\"gPhone\":\"b\",\"gWebsite\":\"c\",\"gEmail\":\"d\",\"gCustAcct\":\"e\",\"gAddress\":\"f\",\"gNotes\":\"g\"}"
//then
var whatEven = {waitNo:""} // nailed it

思想?

0 个答案:

没有答案