角。js美元http。岗位不工作-没有错误

时间:2022-01-18 11:42:41

I'm trying to submit a new comment using $http. You might have guessed it from the title: it's not working. I tried the shore version and the long version, both fail. No console error.

我尝试使用$http提交一个新的评论。你可能已经从标题中猜到了:它不工作。我尝试了shore版本和long版本,都失败了。没有控制台错误。

This is my code:

这是我的代码:

$scope.comment = {};
$scope.comment["comment"] = message; // message defined somewhere else

$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
    .success(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
    .error(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
}

Anyone got any idea on how to make this work? Thanks!

有人知道怎么做吗?谢谢!

UPDATE:

更新:

I'm doing it as a Jquery ajax call now, which is working fine. It'd be nice to get it to work with angular though. This is my JQuery code however:

我现在用Jquery ajax调用它,它运行得很好。让它和角一起工作是很好的。这是我的JQuery代码:

            var request = $.ajax({
                url: '/api/items/'+$scope.item.id+'/comments',
                type: "POST",
                data: JSON.stringify($scope.comment),
                contentType: 'application/json; charset=utf-8',
                dataType: "json"
            });

            request.done(function(msg) {
                console.debug("saved comment", $scope.comment);
            });

            request.fail(function(jqXHR, textStatus) {
                alert( "Request failed: " + textStatus );
            });

If anyone has any ideas how to angularify this please tell me, would be nice to do it the proper way....

如果任何人有任何想法如何angularify这个请告诉我,就好了这样做的正确方法....

9 个解决方案

#1


6  

Have you tried specifically setting the Content-Type before making the request?

在提出请求之前,您是否尝试过具体设置内容类型?

I had the same issue and setting Content-Type fixes it.

我也遇到了同样的问题,设置Content-Type修复了它。

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

#2


5  

Maybe this will help you. I had similiar problem with get call : AngularJS $http not firing get call

也许这对你有帮助。我在get call上遇到了类似的问题:AngularJS $http不触发get call

I solved it with solution, found here https://github.com/angular/angular.js/issues/2794#issuecomment-18807158, so I wraped my call function with $scope.$apply.

我用解决方案解决了这个问题,在这里找到了https://github.com/angular/angular/angular.js/es/2794 # ecom- 18807158,所以我用$scope来包装我的调用函数,$apply。

$scope.$apply(function() {
                console.log('Klic na ID ' + data.context.id);
                $scope.commonController.getData('orgunit/' + data.context.id + '?jsonDepth=3')
                    .success(function(workpositionData,status,headers,config) {
                        console.log('Klic na ID ' + data.context.id + ' OK');
                        $scope.workPositions = workpositionData.workPositions;
                    }).error(function(data,status,headers,config) {
                        commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
                    });
            });

#3


3  

I think, you just forgot some cruel ")" try

我想,你只是忘记了一些残酷的东西)“试试。

$http.post('/api/ideas/'+$scope.item.id+'/comments', $scope.comment)
   .success(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   })  //<-- damn ")"
   .error(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   }) //<--- damn ")"
 }

I did not test it yet, but I bet, this is the error regards

我还没有测试它,但我打赌,这是错误的方面

#4


1  

If you are using angular 1.1.4+ check this thread https://github.com/angular/angular.js/issues/2431.

如果您正在使用角1.1.4+检查这个线程https://github.com/angular/angular.js/issues/2431。

Basically if you are calling $http outside of Angular context you need to wrap $http callback in $scope.$apply. For more details look into https://github.com/angular/angular.js/issues/2794#issuecomment-18807158

基本上,如果你在角上下文中调用$http,你需要在$scope中包装$http回调。$apply。要了解更多细节,请查看https://github.com/angular/angular.js/issues/2794#issue -18807158

#5


0  

You need to initialise the controller with the $http object. Same goes for the other Angular services you wish to use.

您需要用$http对象初始化控制器。对于您希望使用的其他角度服务也是如此。

E.g.:

例如:

function myCtrl($scope, $http) {
  // controller code
}

#6


0  

I was struggling with the same problem. The way I solved it was to use .then() and registering the success and error callbacks as arguments. In your case:

我也在和同样的问题做斗争。我解决它的方法是使用then()并将成功和错误回调注册为参数。在你的例子:

$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
  .then(
    // success callback
    function(response) {
      console.debug("success - saved comment", $scope.comment);
    },
    // error callback
    function(response) {
      console.debug("error - saved comment", $scope.comment);
   }
);

#7


0  

You can try.

你可以试一试。

Angular js post call:

角js post调用:

$http({
        url:'/api/ideas/'+$scope.item.id+'/comments', $scope.comment,
        method : 'POST'
    }).success(function(data, status, headers, config){
    }).error(function(data, status, headers, config){
    });

Rest Controller:

其他控制器:

@RequestMapping
    (value = "/api/ideas/{item_id}/comments/{comment}", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public void updateComments(@PathVariable String item_id,@PathVariable String comment) {
        }

#8


-1  

$http.post('/api/ideas/'+$scope.item.id+'/comments', $scope.comment).success(function(data, status, headers, config) {}).error(function(data, status, headers, config) {});

#9


-1  

angular.module('RestService', [

]).factory('$RestService', [
    '$http', 'ApiUrl',
    function ($http, ApiUrl) {
        return {          
            Functioname: function (request) {
                var Requesturl = ApiUrl + "/Functionname";//www.test.com/getuserdata
                return this.Post(Requesturl, request);//request :parameter which you want to pass in post request
            },
            Post: function (ResultUrl, RequestParameterData) {
                var PostResponse = $http({
                    url: ResultUrl,
                    method: "POST",
                    dataType: "",
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
                    data: RequestParameterData
                });
                return PostResponse;
            },
            Get: function (ResultUrl, RequestParameterData) {
                var PostResponse = $http({
                    url: ResultUrl,
                    method: "GET",
                    dataType: "",
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
                    data: RequestParameterData
                });
                return PostResponse;
            }

        };
    }


]);

#1


6  

Have you tried specifically setting the Content-Type before making the request?

在提出请求之前,您是否尝试过具体设置内容类型?

I had the same issue and setting Content-Type fixes it.

我也遇到了同样的问题,设置Content-Type修复了它。

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

#2


5  

Maybe this will help you. I had similiar problem with get call : AngularJS $http not firing get call

也许这对你有帮助。我在get call上遇到了类似的问题:AngularJS $http不触发get call

I solved it with solution, found here https://github.com/angular/angular.js/issues/2794#issuecomment-18807158, so I wraped my call function with $scope.$apply.

我用解决方案解决了这个问题,在这里找到了https://github.com/angular/angular/angular.js/es/2794 # ecom- 18807158,所以我用$scope来包装我的调用函数,$apply。

$scope.$apply(function() {
                console.log('Klic na ID ' + data.context.id);
                $scope.commonController.getData('orgunit/' + data.context.id + '?jsonDepth=3')
                    .success(function(workpositionData,status,headers,config) {
                        console.log('Klic na ID ' + data.context.id + ' OK');
                        $scope.workPositions = workpositionData.workPositions;
                    }).error(function(data,status,headers,config) {
                        commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
                    });
            });

#3


3  

I think, you just forgot some cruel ")" try

我想,你只是忘记了一些残酷的东西)“试试。

$http.post('/api/ideas/'+$scope.item.id+'/comments', $scope.comment)
   .success(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   })  //<-- damn ")"
   .error(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   }) //<--- damn ")"
 }

I did not test it yet, but I bet, this is the error regards

我还没有测试它,但我打赌,这是错误的方面

#4


1  

If you are using angular 1.1.4+ check this thread https://github.com/angular/angular.js/issues/2431.

如果您正在使用角1.1.4+检查这个线程https://github.com/angular/angular.js/issues/2431。

Basically if you are calling $http outside of Angular context you need to wrap $http callback in $scope.$apply. For more details look into https://github.com/angular/angular.js/issues/2794#issuecomment-18807158

基本上,如果你在角上下文中调用$http,你需要在$scope中包装$http回调。$apply。要了解更多细节,请查看https://github.com/angular/angular.js/issues/2794#issue -18807158

#5


0  

You need to initialise the controller with the $http object. Same goes for the other Angular services you wish to use.

您需要用$http对象初始化控制器。对于您希望使用的其他角度服务也是如此。

E.g.:

例如:

function myCtrl($scope, $http) {
  // controller code
}

#6


0  

I was struggling with the same problem. The way I solved it was to use .then() and registering the success and error callbacks as arguments. In your case:

我也在和同样的问题做斗争。我解决它的方法是使用then()并将成功和错误回调注册为参数。在你的例子:

$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
  .then(
    // success callback
    function(response) {
      console.debug("success - saved comment", $scope.comment);
    },
    // error callback
    function(response) {
      console.debug("error - saved comment", $scope.comment);
   }
);

#7


0  

You can try.

你可以试一试。

Angular js post call:

角js post调用:

$http({
        url:'/api/ideas/'+$scope.item.id+'/comments', $scope.comment,
        method : 'POST'
    }).success(function(data, status, headers, config){
    }).error(function(data, status, headers, config){
    });

Rest Controller:

其他控制器:

@RequestMapping
    (value = "/api/ideas/{item_id}/comments/{comment}", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public void updateComments(@PathVariable String item_id,@PathVariable String comment) {
        }

#8


-1  

$http.post('/api/ideas/'+$scope.item.id+'/comments', $scope.comment).success(function(data, status, headers, config) {}).error(function(data, status, headers, config) {});

#9


-1  

angular.module('RestService', [

]).factory('$RestService', [
    '$http', 'ApiUrl',
    function ($http, ApiUrl) {
        return {          
            Functioname: function (request) {
                var Requesturl = ApiUrl + "/Functionname";//www.test.com/getuserdata
                return this.Post(Requesturl, request);//request :parameter which you want to pass in post request
            },
            Post: function (ResultUrl, RequestParameterData) {
                var PostResponse = $http({
                    url: ResultUrl,
                    method: "POST",
                    dataType: "",
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
                    data: RequestParameterData
                });
                return PostResponse;
            },
            Get: function (ResultUrl, RequestParameterData) {
                var PostResponse = $http({
                    url: ResultUrl,
                    method: "GET",
                    dataType: "",
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
                    data: RequestParameterData
                });
                return PostResponse;
            }

        };
    }


]);