将$ http请求迁移到AngularJS V1.6

时间:2017-04-10 14:32:54

标签: angularjs angular-promise angular-http angularjs-1.6

我将angularjs包升级到1.6.3,发现"SELECT DeptName FROM Departments inner JOIN Faculties ON Faculties.ID = Departments.FacID where FacID='" _ & ListBox1.SelectedValue[columnIndex].ToString() & "'" .success函数已弃用 已删除。现在使用.error.then后,只有.catch执行。我正在努力找出这次请求失败的原因。

我的初始工作代码是:

.catch

然后我将其更改为以下内容:

if ($scope.IsDinamicReport) {
            $http({
                method: "POST",
                url: "/api/DynamicReport/Post?pageNumber=" + $scope.PageNum + "&orderbyColumn=" + $scope.orderByColumn + "&sortOrder=" + $scope.sortOrder
                    + "&showNumberPagingStats=" + $scope.showNumberPagingStats,
                contentType: "application/json",
                data: $scope.report
            }).success(function (result) {
                angular.copy(result, $scope.dynamicReport);

                if (!$scope.dynamicReport.Error) {
                    $scope.HideDynamicRepFunctions = false;
                    $scope.exportColumnSelected = $scope.dynamicReport.Columns[0]; //Set default for export drop down
                    //TABLE SIZING
                    var persentage = $scope.returnTableSizing(result.Columns.length);

                    $('[data-table=container]')
                        .css('margin-left', '25px')
                        .css('padding-right', '25px')

                        .css('width', persentage)
                        .css('max-width', persentage);  
                }
                else
                    alert("Error occured while generating the report, please contact helpdesk.");

            }).error(function (data) {
                alert("An error occured while generating the report, please try again.");
            });
        }

如果您在此处看不到错误,我该如何调试出错?我唯一改变的就是不推荐使用的功能

3 个答案:

答案 0 :(得分:1)

请改用标准.then.catch承诺方法,但请注意方法签名和返回值不同:

之前:

$http(...)
  .success(function onSuccess(data, status, headers, config) {
  // Handle success
  //...
}).error(function onError(data, status, headers, config) {
  // Handle error
  //...
});

后:

$http(...)
  .then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    //...
    return <some value>;
}).catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    //...
    throw <some error>;
});

有关详细信息,请参阅AngularJS Developer Guide - Migrating from 1.5 to 1.6

对于代码,result值是response对象的属性:

$http({
    method: "POST",
    url: "/api/DynamicReport/Post?pageNumber=" + $scope.PageNum + "&orderbyColumn=" + $scope.orderByColumn + "&sortOrder=" + $scope.sortOrder
        + "&showNumberPagingStats=" + $scope.showNumberPagingStats,
    contentType: "application/json",
    data: $scope.report
//}).success(function (result) {
}).then(function (response) {
    //RESULT is a property of response
    var result = response.data;
    angular.copy(result, $scope.dynamicReport);

    if (!$scope.dynamicReport.Error) {
        $scope.HideDynamicRepFunctions = false;
        $scope.exportColumnSelected = $scope.dynamicReport.Columns[0]; //Set default for export drop down
        //TABLE SIZING
        var persentage = $scope.returnTableSizing(result.Columns.length);

答案 1 :(得分:0)

使用调试器;在你的源代码中。 按F12,F8,F10。这将有助于

答案 2 :(得分:0)

在角度doc中,$http服务返回一个具有两个回调,成功和失败的承诺(没有catch方法)。

在您的代码中,您尝试使用catch来处理拒绝,但是在doc中没有报告。

这是提供的语法,请尝试相应地修改您的代码。

$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.
  });

来源:https://docs.angularjs.org/api/ng/service/ $ http