简体   繁体   English

在异步回发范围函数上获取JavaScript错误

[英]Getting JavaScript error on asynchronous postback scope function

I am getting the following error reported on angular.min.js 我在angular.min.js上报告了以下错误

0x800a1391 - JavaScript runtime error: 'Error' is undefined 0x800a1391-JavaScript运行时错误:“错误”未定义

with the following code: 使用以下代码:

Javascipt: Javascipt:

function Sucess() 
{
    //close
}

function Save() 
{
    var e = document.getElementById('FormDiv');
    scope = angular.element(e).scope();
    scope.Apply(Sucess)
}

My Angular scope function: 我的角度范围函数:

function RolesCtrl($scope, $http, $location) 
{
    $scope.Apply = function (CallBackSucess) {

    var userId = getQSP('uid', decode(document.URL));
    $http({
    method: 'POST', url: 'MultiRole.aspx/Apply',
    data: {}
    }).
    success(function (data, status, headers, config) {
        // this callback will be called asynchronously
        CallBackSucess();
        $scope.f = data.d;
        }).
    error(function (data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    $scope.name = 'error';
    })
    }
}

Everything seems to be working fine until CallBackSucess() call is made which throws the error: 在调用CallBackSucess()引发错误之前,一切似乎都工作正常:

0x800a1391 - JavaScript runtime error: 'Error' is undefined 0x800a1391-JavaScript运行时错误:“错误”未定义

The CallBackSucess argument is passed to the .Apply() method. CallBackSucess参数传递给.Apply()方法。 It is not passed to the .success() method - they are chained methods with separate scopes. 它不会传递给.success()方法-它们是具有单独范围的链接方法。 Thus, in the .success() callback function, CallbackSucess() is not defined when you try to call it and thus you get an error. 因此,在.success()回调函数中,当您尝试调用CallbackSucess()时未定义它,从而导致错误。

Also, do you really mean to spell Sucess incorrectly? 另外,您真的要错误地拼写Sucess吗?

FYI, I had to format your code like this in order to see what was actually going on: 仅供参考,为了查看实际情况,我必须像这样格式化您的代码:

function RolesCtrl($scope, $http, $location) {
    $scope.Apply = function (CallBackSucess) {
        var userId = getQSP('uid', decode(document.URL));
        $http({
            method: 'POST', 
            url: 'MultiRole.aspx/Apply',
            data: {}
        }).success(function (data, status, headers, config) {
            // this callback will be called asynchronously
            CallBackSucess();
            $scope.f = data.d;
        }).error(function (data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            $scope.name = 'error';
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM