我是angularjs的新手,我正在尝试进行$ http.post调用。这是我的HTML代码:
<div class="simple-controller" ng-controller="Simple.SimpleController">
<button type="button" id="create" name="create" data-toggle="modal"
data-target="#myModal" style="margin-left: 45%; margin-bottom: 1%; margin-top: 2%">CREATE</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Details</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="usr">Name:</label> <input type="text"
class="form-control" id="usr">
</div>
<div class="form-group">
<label for="pwd">Service:</label> <input type="text"
class="form-control" id="service">
</div>
</div>
<div class="modal-footer">
<button type="button" onclick="addData()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
我正在尝试使用名称和服务输入类型作为路径参数对本地的休息服务进行调用。所以我的网址应该是这样的 的 http://localhost:8181/cxf/authorization/addData/Siddhu/jhg
这是我用于发布电话的js代码:
function addData($scope, $http) {
var name = document.getElementById("usr").value;
var service = document.getElementById("service").value;
var url = 'http://localhost:8181/cxf/authorization/addData';
alert('URL ==== '+url);
$http.post(url,{name:name, service:service})
.success(function(data) {
$scope.data = data;
alert('THE DATA IS === '+$scope.data);
});
}
我得到Uncaught TypeError: Cannot read property 'post' of undefined error
。
有人能告诉我拨打电话的正确方法吗?
答案 0 :(得分:2)
你的addData函数需要2个参数($ scope和$ http)但是从html调用它时你没有传入。
我怀疑你已经将$ http服务连接到你的控制器,但是后来用方法参数取代了它。
答案 1 :(得分:0)
这就是我为那些寻求代码的人解决问题的方法。而不是onclick我添加了ng-click指令:
<button type="button" ng-click="addData()" class="btn btn-primary">Save</button>
我的功能代码:
$scope.addData = function() {
var name = $scope.usr;
var service = $scope.service;
var url = 'http://localhost:8181/cxf/authorization/addData/'+name+'/'+service;
$http.post(url)
.success(function(data) {
if(data == 'Updated'){
fetchData();
$('#myModal').modal("hide");
}
else{
alert("Details not added");
$('#myModal').modal("hide");
}
});
}