我想使用 cordova
和 angularjs
将数据发布到上述链接(尽管使用了 webservice
)。我在两者之间使用警报来了解哪些功能正常工作。 sign Up Function 不起作用,它正在进入功能但没有进入请求方法来发布数据。
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>
如何将数据发布到 webservice
( http://hmkcode.appspot.com/jsonservlet )
最佳答案
您必须按以下方式更改您的 Controller 。由于你使用的是新版本的angularjs,将.success
改为.then
。而且当你使用表单时,最好使用 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>
关于android - 我想使用 angularjs 和 cordova 将数据发布到 http ://hmkcode. appspot.com/jsonservlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365852/