为什么AngularJS发送双网络请求?

时间:2022-04-04 21:01:18

I have the code shown below in a controller for an AngularJS Single Page Application. When this page runs and I check Developer Tools Network (in IE, Chrome, and Firefox) it always shows 2 successful GET calls to Tables, and two empty OPTIONS calls to Tables. Why are there 2 GET calls? Is that normal or did I do something wrong in my code? Also, why does it issue the 2 OPTIONS calls?

我在AngularJS单页面应用程序的控制器中有如下所示的代码。当这个页面运行并且我检查开发人员工具网络(在IE,Chrome和Firefox中)时,它总是显示2个成功的GET调用表,以及两个空的OPTIONS调用表。为什么有2个GET电话?这是正常的还是我在代码中做错了什么?另外,为什么它会发出2个OPTIONS调用?

"use strict";

app.controller('AdminController', function ($scope, $http)
{
    $scope.$parent.Title = "Admin";

    var url = $scope.$parent.BaseUrl + "Tables";
    $http.get(url)
        .then(function mySuccess(response)
        {
            $scope.MyTables = response.data;
        });
});

1 个解决方案

#1


4  

This has happened to me in the past, the issue was that I had defined my controller twice, once in the route:

这在过去发生在我身上,问题是我已经定义了我的控制器两次,一次在路线中:

.state('app.state', {
  url: '/state',
  controller: 'SomeCtrl',
  templateUrl: 'views/state.html'
})

and then defined it again in my view HTML:

然后在我的视图HTML中再次定义它:

<div ng-controller="SomeCtrl"></div>

As far as I am aware, you should only define it in one or the other.

据我所知,你应该只在一个或另一个中定义它。

#1


4  

This has happened to me in the past, the issue was that I had defined my controller twice, once in the route:

这在过去发生在我身上,问题是我已经定义了我的控制器两次,一次在路线中:

.state('app.state', {
  url: '/state',
  controller: 'SomeCtrl',
  templateUrl: 'views/state.html'
})

and then defined it again in my view HTML:

然后在我的视图HTML中再次定义它:

<div ng-controller="SomeCtrl"></div>

As far as I am aware, you should only define it in one or the other.

据我所知,你应该只在一个或另一个中定义它。