【问题标题】:Angular HTTP does not work on first clickAngular HTTP 在第一次点击时不起作用
【发布时间】:2017-05-19 22:40:56
【问题描述】:

单击按钮时,我正在调用方法 Getdata();但问题是第一次加载页面时,如果我单击按钮,则会收到“HTTP 错误”响应。但是如果我再次单击该按钮,则会显示成功响应。

HTML

<button type="submit" class="btn btn-primary form-inline inline-Magrin" ng-click="Getdata()">Search</button>

JS

$scope.Getdata = function () {
    if (($scope.NumberOfrecords > $scope.TotalRecords) && ($scope.TotalRecords != 0)) {
        alert("Search record count should not be greater than total records");
        return;
    }
    debugger;
    $http({
        url: '/scrap/Resultdata',
        method: "GET",
        params: {
            Searchbox: $scope.Searchbox,
            Category: $scope.Category,
            NumberOfrecords: $scope.NumberOfrecords
        }
    }).then(
        function successCallback(response) {
            response = $scope.filterRecord(response);
            debugger;
            $scope.data = response.data.ResponseItems;
            $scope.TotalRecords = response.data.TotalResults;
            $scope.tableParams = new NgTableParams({
                page: 1,
                count: 10
            }, {
                data: $scope.data
            });
        },
        function errorCallback(response) {
            debugger;
            alert("error");
        });
}

【问题讨论】:

  • 控制台中的错误不是针对/scrap/Resultdata 的请求。查看您的网络流量,看看当请求有效和无效时有什么区别。我猜你的一些 $scope 变量第一次没有初始化
  • 试试type="button"而不是type="submit"
  • 你在某个地方弄乱了你的 url,但不是在插入的代码中。

标签: javascript angularjs


【解决方案1】:

按钮类型是“提交”,应该是“按钮”

<button type="button" class="btn btn-primary form-inline inline-Magrin" ng-click="Getdata()">Search</button>

【讨论】:

    【解决方案2】:

    如果你有提交作为类型,那么你需要在html中添加标签,

    和形式-------- OnSubmit=GetData()

    请尝试改用 type=button

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多