无法使用AngularJS在我的http帖子中发送字符串

时间:2015-07-02 19:54:09

标签: angularjs string http post

我是AngularJS的新手,我在http帖子中发送字符串时遇到问题。我目前正在使用这种格式发送http帖子,这是我在网上的一个例子,

        $http({

            method: 'POST',

            url: 'http://example.com/',

            data: { 'ver':$scope.version, 

            'deviceTime':moment().valueOf(), 

            'name':$scope.$storage.name, 

            'email':$scope.$storage.email, 

            'phone':$scope.$storage.phone, 

            'device':$scope.$storage.device, 

            'endTime': $scope.$storage.end },

            headers: { 'Content-Type':'application/x-www-form-urlencoded' }

        })

    };

虽然这是作为对象发送的。当我尝试以字符串格式发送信息时:

        $http({

            method: 'POST',

            url: 'http://example.com/',

            data:'ver=' + $scope.version,

            headers: { 'Content-Type':'application/x-www-form-urlencoded' }

        })
    };

该帖子未出现在服务器中。关于如何单独发送字符串的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试使用内置的post方法:

  $http.post("some url",{'param1':paramvalue,'param2':param2value})

你的paramvalue应该是一个没有问题的字符串。请注意,如果您的字符串是" name = value"然后你会得到类似" param1 = name = value"和" name = value"将在您的网址中进行编码。