我想使用angularjs& cordova将数据发布到http://hmkcode.appspot.com/jsonservlet

时间:2017-07-28 05:55:39

标签: android angularjs json cordova postdata

我想使用webservicecordova将数据发布到上述链接(尽管使用angularjs)。我之间使用过警报来了解哪些功能正常工作。注册功能不起作用,它正在进入功能但没有输入请求方法来发布数据。 request.success(function(data)

中是否有任何错误
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport"content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <meta name="msapplication-tap-highlight" content="no" />
    <title>Post Data</title>
</head>
<body>
    <div ng-app="HelloApp" ng-controller="HelloCtrl">
        <form role="form"  action="#">
            <div>
                Name: <input type="text" ng-model="name"><br>
                Country: <input type="text" ng-model="country"><br>
                Twitter: <input type="text" ng-model="twitter"><br>
                <button ng-click="signUp()">Save</button><br>
                <span>{{responseMessage}}</span>           
        </form>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
    <script>
        alert('Before controller');
        var helloApp = angular.module("HelloApp", []);
        helloApp.controller("HelloCtrl", [ '$scope','$http', function($scope,$http) 
        {
            alert('Entered Controller');
            $scope.name = "Calvin Hobbes";
            $scope.country = "US";
            $scope.twitter = "as@gmail.com";

            $scope.signUp = function () {
                /*  $scope.Message = "Button clicked."*/
                alert("Button CLicked");;
                var request = $http({
                    method: "post",
                    dataType:'json',
                    url: "http://hmkcode.appspot.com/jsonservlet",
                    crossDomain : true,
                    data: {
                        'name': $scope.name,
                        'country': $scope.country,
                        'twitter': $scope.twitter
                    },
                        headers: { 'Accept':' text/plain','Content-Type': 'textjson' }
                });
                /* Successful HTTP post request or not */
                request.success(function(data) {
                    alert("success function");
                    if(data == "1"){
                        $scope.responseMessage = "Successfull";
                    }
                    if(data == "2"){
                        $scope.responseMessage = "failed";
                    } else if(data == "0") {
                        $scope.responseMessage = "Error";
                    }  
                });
            }
        }]);
   </script>
  </body>
  </html>

如何将数据发布到webservicehttp://hmkcode.appspot.com/jsonservlet

1 个答案:

答案 0 :(得分:1)

您必须按以下方式更改控制器。由于您使用的是新版本的angularjs,请将TextViews更改为.success。而且当您使用表单时,最好使用.then

<强> JS

ng-submit

<强> HTML

$scope.signUp = function () {
                /*  $scope.Message = "Button clicked."*/
                alert("Button CLicked");;
                var request = $http({
                    method: "post",
                    dataType:'json',
                    url: "http://hmkcode.appspot.com/jsonservlet",
                    crossDomain : true,
                    data: {
                        'name': $scope.name,
                        'country': $scope.country,
                        'twitter': $scope.twitter
                    },
                        headers: { 'Accept':' text/plain','Content-Type': 'application/json' }
                });
                /* Successful HTTP post request or not */
                request.then(function(data) {
                    alert("success function");
                    if(data.data == "1"){
                        $scope.responseMessage = "Successfull";
                    }
                    if(data.data == "2"){
                        $scope.responseMessage = "failed";
                    } else if(data.data == "0") {
                        $scope.responseMessage = "Error";
                    }  
                });
            }