当发现.then不是函数时,如何解决?

时间:2019-01-29 20:38:05

标签: javascript jquery angularjs

对于我的以下代码:

$http({
    method: "POST",
    url: applicationUrl + '/Order/GetTax',
    params: { ShippingId: shippreference },
    contentType: "application/json; charset=utf-8",
    dataType: "json"
}).success(function (data) {
    if (data != "ADDED_FAIL") {
        $scope.getTaxData = data;
        $scope.shippingHandling = data.TotalShipping;
        $scope.Tax = data.TotalTax;
        $scope.CouponSaving = data.Coupons.length == 0 ? null : data.Coupons[0].Amount;
        $scope.CouponSavingDesc = data.Coupons.length == 0 ? null : data.Coupons[0].Description;
    } else {
        $('#popLoader').hide();
        jAlert('Internal Technical Error. Please try again!');
        taxErrorFlag = false;
        return false;
    }
    $('#popLoader').hide();
}).error(function (x) {
    jAlert(x);
});

我将网站升级到jquery-2.2.4.js和AngularJS v1.7.6。并出现以下错误:

TypeError: $http.get(...).success is not a function

要修复错误,我将.success替换为.then 我这样做是根据以下答案的建议: $http.get(...).success is not a function

但是,然后我得到了错误。

  TypeError: $http(...).then(...).error is not a function

我不确定下一步该怎么做...

1 个答案:

答案 0 :(得分:-1)

这是因为.then接受两个参数,即成功和错误回调。您正在尝试在.then函数上调用error(),从而引发错误。试试这个:

$http({
    method: "POST",
    url: applicationUrl + '/Order/GetTax',
    params: { ShippingId: shippreference },
    contentType: "application/json; charset=utf-8",
    dataType: "json"
}).then(function (data) {
    if (data != "ADDED_FAIL") {
        $scope.getTaxData = data;
        $scope.shippingHandling = data.TotalShipping;
        $scope.Tax = data.TotalTax;
        $scope.CouponSaving = data.Coupons.length == 0 ? null : data.Coupons[0].Amount;
        $scope.CouponSavingDesc = data.Coupons.length == 0 ? null : data.Coupons[0].Description;
    } else {
        $('#popLoader').hide();
        jAlert('Internal Technical Error. Please try again!');
        taxErrorFlag = false;
        return false;
    }
    $('#popLoader').hide();
}, function (x) {
    jAlert(x);
});