然后Angularjs $ http无法正常工作

时间:2017-10-04 05:58:27

标签: angularjs

我在回复中得到“真”的值。为什么我的$ http .then中的debuggeralert以及AccessGranted()未被调用。以下是我的剧本:

app.controller("LoginController", function($scope, $http) {
    $scope.btnText = "Enter";
    $scope.message = "";

$scope.login = function() {
    $scope.btnText = "Please wait...";
    $scope.message = "We're logging you in.";

    $http({
        method: 'post',
        url: '/Login/Login',
        data: $scope.LoginUser
    }).then(function (response) {
            debugger;
            alert(response.data);
            if (response.data == "True") {
              AccessGranted();
            } else {
            $scope.message = response.data;
            $scope.btnText = "Enter";
        } 
    },
    function (error) {
        $scope.message = 'Sending error: ' + error;
    });
}

$scope.AccessGranted = function() {
    window.location.pathname("/Home/HomeIndex");
}
});

这是在我的HomeController中

public ActionResult HomeIndex()
{
    var am = new AuditManager();
    var auditModel = new AuditModel()
    {
        AccountId = 0,
        ActionDateTime = DateTime.Now,
        ActionName = "Home",
        ActionResult = "Redirected to Home"
    };
    am.InsertAudit(auditModel);

    return View("Index");
}

请参阅图片以获取我的回复。 enter image description here

4 个答案:

答案 0 :(得分:0)

看起来你的做法是错误的

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

答案 1 :(得分:0)

试试这个,

$http({
  method: 'post',
  url: '/Login/Login',
  data: $scope.LoginUser
})
.then(function (response) {
    console.log(response);
},
function (error) {
    console.log(error);
});

并检查浏览器控制台是否有日志或任何错误

答案 2 :(得分:0)

确保响应是application / json内容类型,内容是json。

你也可以编写自己的httpProvider来检查来自服务器的结果

module.config(['$httpProvider', function ($httpProvider) {
   ...

答案 3 :(得分:-1)

我建议你这样编码,而不是那时只要成功,就会调用成功部分。

 $http.get('/path/').success(function (data) {
        $scope.yourdata = data.data;
        //console.log($scope.yourdata);
    }).error(function (error){
//error part
    });