Angular JS $ http get调用在Chrome中有效,并在FireFox

时间:2017-01-11 21:56:24

标签: c# angularjs asp.net-mvc google-chrome firefox

我有一个使用AngularJS的MVC应用程序。我们不使用WebAPI。因此,angularJS只会调用MVC控制器。这些调用在IE&铬。然而,在Firefox和Safari中,我尝试了几个小时来启动并运行而没有任何结果。正如您在下面的代码中看到的,我有一个LoginController.js文件和LoginService.js文件。 service.js文件将promise对象返回给控制器。在firefox出错时,数据为null,标题为emptry,statusText为空,status为-1。我不知道这与CORS有什么关系。但我没有任何人在这里,因为该服务只是调用MVC控制器。

LoginController.JS file

   app.controller('LoginController',
    function ($scope, LoginService) {
        $scope.schoolList = null;
        $scope.SelectedDistrictId = null;
        $scope.isFLUser = $("#isFLUser").val();
        $scope.User =
        {
            userName: "",
            password: "",
            Email: null,
            firstName: "",
            lastName: "",
            roleId: null,
            districtId: null,
            schoolId: null,
            districtName: "",
            schoolName: "",
            isFL: $("#IsFL").val()
        }
        $scope.validateUser = function () {
            $("#LoginErrorSpan").hide();
            var form = $('form');
            $.validator.unobtrusive.parse(form);
            if (form.valid()) {
                $("#spinnerdiv").show();
                debugger;
                var promise = LoginService.validateUser($scope.User);
                promise.then(function (result) {
                    debugger;
                    $("#spinnerdiv").hide();
                    if (result.data.InvalidUser === false) {
                        if (result.data.PwdchangeCount > 0) {
                            window.location.href = "/Login/Welcome";
                        }
                        else {
                            window.location.href = "/Login/ChangePassword";
                        }
                    } else {
                        $("#LoginErrorSpan").show();
                    }
                },
                    function (result) {
                        debugger;
                        console.log(result.headers());
                    });
            }
        }
    });

LoginService.JS文件

var app = angular.module('app', []);
app.factory('LoginService',
    function ($http) {
        return {
            validateUser: function (scopeUser) {
                return $http({
                    url: "/Login/ValidateUser",
                    method: "GET",
                    params: {
                        userName: scopeUser.userName,
                        //password: encodeURIComponent(scopeUser.password),
                        password: scopeUser.password,
                        isFL: scopeUser.isFL
                    }
                });

            }
        }
    });

我一直在尝试使用我在网上找到的所有解决方案来调试这8小时。 Firebug

1 个答案:

答案 0 :(得分:0)

也许如果您只是直接从浏览器打开html文档,要解决此问题,您需要从Web服务器提供代码并在localhost上访问它。通过IDE(Visual Studio,Eclipse等)将该文件作为Web站点项目打开。

AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https