android - I want to post data to http://hmkcode.appspot.com/jsonservlet using angularjs&cordova -
i want post data above link(in spite of using webservice
) using cordova
, angularjs
. have used alerts in between know functions working properly. sign function not working, entering function not entering request method post data. there error in 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>
how can post data webservice
(http://hmkcode.appspot.com/jsonservlet)
you have change controller following way. since using new version of angularjs, change .success
.then
. , when using form, better use ng-submit
js:
$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"; } }); }
html:
<form role="form" ng-submit="signup()"> <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 type="submit">save</button><br> <span>{{responsemessage}}</span> </form>
Comments
Post a Comment