HTTP post调用在angularjs中不起作用
http post call not working from angularjs
我是AngularJS新手。我已经包含了控制器、服务和对rest服务的调用的代码。请告知为什么电话没有到达休息服务。
我的代码如下:app.config(function ($routeProvider) {
$routeProvider
.when('/addNewNote', {
controller: 'AddNewNoteController',
templateUrl:'views/addNote.html'
})
angularjs控制器如下
app.controller('AddNewNoteController', ['$scope','savenote', function($scope,savenote) {
savenote.success(function(eData){
$scope.msg = eData;
调用http post rest服务的angular服务
app.factory('savenote',['$http',function($scope,$http){
return $http({
method: 'POST',
url: <url is pasted here>,
dataType: 'json',
data: {
"title" : "123dddd",
"contents" : "123ddddtttttt"
},
headers: { 'Content-Type': 'application/json; charset=UTF-8' }
})
}]);
这是剩下的服务
@Path("/savenote")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserMessages saveNewNote(Note note) throws IOException {
.....
}
您忘记了$scope
的类型提示:
app.factory('savenote',['$scope', '$http', function($scope,$http){
同时,你的工厂应该返回一个具有以下方法的对象:
app.factory('savenote', ['$scope', '$http', function ($scope, $http) {
return {
save: function () {
return $http({
method: 'POST',
url: "<url is pasted here>",
dataType: 'json',
data: {
"title": "123dddd",
"contents": "123ddddtttttt"
},
headers: {'Content-Type': 'application/json; charset=UTF-8'}
});
}
};
}]);
并按如下方式使用:
savenote.send().then(function(eData) {});
另外,正如@SarjanDesai在他的评论中所说,$scope
没有在你的工厂使用,所以你应该删除它。
savenote
返回$http
对象,该对象具有调用成功和失败回调函数。
用下面的语句更新你的控制器函数:
savenote.then(function successCallback(eData) {
$scope.msg = eData;
}
$http遗留的承诺方法的成功和错误已经弃用。请使用标准的then方法。如果httpProvider美元。useLegacyPromiseExtensions设置为false方法将抛出$http/legacy错误。
相关文章:
- 我的AngularJS表达式没有'不起作用
- AngularJS指令部分应用的函数don'不起作用
- AngularJS ng include dons'不起作用
- AngularJs和Laravel的Pretty Urls不起作用
- Google Maps API OverlayView()在AngularJS指令中不起作用
- AngularJS ng include won'不知什么原因不起作用
- AngularJS没有'不起作用
- Grunt concat js angularjs 不起作用
- angularjs ng点击运行时标记不起作用
- AngularJS:$on dons'不起作用
- AngularJS-简单的教程不起作用
- 输入类型范围在angularjs重复内不起作用
- 数据绑定在特定情况下不起作用(AngularJS)
- 路由不起作用 AngularJS,只是添加一个哈希
- 在 jsfiddle 中拖放表中的行不起作用 - angularjs
- 提交按钮不起作用 AngularJS
- $location.search() 不起作用 - AngularJs 1.5
- 单击按钮时获取单选按钮值不起作用angularjs
- Ng-repeat不起作用AngularJs
- 提交指令不起作用 AngularJS