从AngularJS发布JSON数据

时间:2017-03-15 12:39:57

标签: angularjs cordova

app.controller("blankCtrl", ['$scope', '$http', '$parse', function($scope, $http, $parse) {
    $scope.login = function() {
        $scope.spice = 'tooo';
        console.log("hiii");
        alert($("#loginform").serialize()); //-------mobile=9860292514&password=123456

        //  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
        // $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON'; 
        var dataObj = {
            mobile: $scope.mobile,
            password: $scope.password
        };
        $http({
                method: 'POST',
                url: 'http://edudux.com/manage/index.php?/api/login',
                data:dataObj,
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .success(function(data, status, headers, config) {
                alert("status" + status); //-----------status200
                alert(angular.isObject(JSON)); // --------true            
                alert(JSON); //-------------[object JSON]

                var a = angular.toJson(dataObj);
                alert(a); //----------{}

                var b = angular.fromJson(dataObj);
                alert(b); //------------[object Object] 

                alert(data); //----------[object Object]
                alert(JSON.stringify(dataObj)); //----------{}
                console.log(JSON.stringify(dataObj));

            })
            .error(function(data, status, headers, config) {
                alert(status);
            });
    };
}]);

我是AngularJS的新手,并使用AngularJS开发新的应用程序。 我尝试使用AngularJS $ http服务器发布JSON对象。但我得到一个空对象作为回应:" {}"

3 个答案:

答案 0 :(得分:0)

Ypu需要在http请求中传递dataObj,如下所示。

app.controller("blankCtrl",['$scope','$http','$parse',function ($scope,$http,$parse) {
 $scope.login = function() {
    $scope.spice = 'tooo';
   console.log("hiii");
 alert($("#loginform").serialize()); 
//  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON'; 
var dataObj = {
            mobile : $scope.mobile,
            password : $scope.password
    };  

var config = {
                headers : {
                    'Content-Type': 'Content-Type':'application/json;'
                }
            }

             $http.post('http://edudux.com/manage/index.php?/api/login', dataObj, config)
            .success(function (data, status, headers, config) {
               alert(JSON.stringify(data));
            })
            .error(function (data, status, header, config) {
                alert('error');
            });

  }

}]);

答案 1 :(得分:0)

我认为这对你有用:

app.controller("blankCtrl", ['$scope', '$http', '$parse','$q', function($scope, $http, $parse,$q) {
    $scope.login = function() {
        $scope.spice = 'tooo';
        console.log("hiii");
        alert($("#loginform").serialize()); //-------mobile=9860292516&password=123456

        //  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
        // $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON'; 
        var dataObj = {
            mobile: $scope.mobile,
            password: $scope.password
        };
     var deferred = $q.defer();
     $http({
        method: 'POST',
        url:  'http://edudux.com/manage/index.php?/api/login',
        data: dataObj,
        headers: {'Content-Type': 'application/json'}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
            console.log(deferred.promise);
            alert(data);

        }).error(function (data, status, headers, config) {
            console.log(data, status, headers, config);

        });

    };
}]);

答案 2 :(得分:0)

执行POST请求的快捷方式。

$scope.login = function () {
  // URL
  var reqURL = "http://edudux.com/manage/index.php?/api/login";

  // data
  var reqData = {
    mobile : $scope.mobile,
    password : $scope.password
  };

  $http.post(reqURL, reqData).then(function (response) {
    // response from server. after login
    console.log(response.data);
  });
};

文档:$http request documentation.

我希望能帮助你解决上面的问题!