【问题标题】:$http.post in angularJS goes in error in without debugging mode only.in debugging mode its works fine.why?angularJS 中的 $http.post 仅在没有调试模式的情况下出错。在调试模式下它工作正常。为什么?
【发布时间】:2016-06-09 05:40:04
【问题描述】:

这是我的 javascript 代码

 $scope.addUser = function () {
                debugger;
                url = baseURL + "AddUser";
                $scope.objUser = [];
                $scope.objUser.push( {
                    "ID": '0',
                    "UserName": $scope.txtUserName,
                    "Password": $scope.txtPassword,
                    "Role":"Non-Admin"
                });

                $http.post(url,$scope.objUser[0])
                    .success(function (data) {
                        debugger;
                        alert("S");
                        window.location = "../View/Login.html";
                    }).error(function () {
                        debugger;
                        alert("e");

                    });
            }

这是我的服务器方法代码

[HttpPost]
        public int AddUser(UserModel user)
        {
            //_entity.Configuration.ProxyCreationEnabled = false;
            tblUser objUser = new tblUser();
            objUser.UserName = user.UserName;
            objUser.Password = user.Password;
            objUser.Role = user.Role;
            _entity.tblUsers.Add(objUser);
            _entity.SaveChanges();
               return objUser.ID;
     }

【问题讨论】:

  • 你应该通过添加超时来尝试
  • 你检查你的服务器方法了吗,它总是返回成功
  • 当我在浏览器中运行没有调试器的项目时,它显示我在 ajax 错误中编写的警告消息。然后它抛出“HTTP 错误 405.0 - 不允许方法”。
  • 它不进入服务器方法,即使我把调试器也放在那里
  • 你也可以发布你的服务器方法

标签: angularjs http-post


【解决方案1】:

您可以使用承诺来获得响应。这可以在服务内部并在您想使用它时调用它。

this.addUser = function (obj) {
        var datosRecu = null;
        var deferred = $q.defer();
        var uri = baseUrl + 'addUser';
        $http({
            url: uri,
            method: 'post',
            data: angular.toJson(obj)
        }).then(function successCallback(response) {
            datosRecu = response;
            deferred.resolve(datosRecu);
        }, function errorCallback(response) {
            datosRecu = response;
            deferred.resolve(datosRecu);
        });
        return deferred.promise;
    };

.error.success 也已弃用。

PD:$http里面的参数data:对应body。如果你想发送参数,你应该使用params:{}

编辑: 在这里,我给你一个链接,承诺是如何工作的。 Angular promises 基本上这有助于异步处理数据

上面的例子可以在这样的服务中使用

myApp.service('myService', function($q, $http){

// here your services....

});

可以将服务注入到任何控制器内部,以在函数内部提供您想要的数据

myApp.controller('myController', function($scope, myService){

  $scope.list = function(){
      $promise = myService.getAll(); // this will be the name of your function inside your servive
      $promise.then(function(data){
         console.log(data); //here you can se your promise with data like status and messages from the server.
      });
  };

});

希望对你有帮助。

【讨论】:

  • 我认为你是对的。promises 可以在这里使用。但我是 angularJS 的新手,所以我无法理解它的代码。你能给任何链接以更好地理解 promises 吗?
猜你喜欢
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多