简体   繁体   English

jQuery Ajax成功不会触发,而是完成事件

[英]Jquery Ajax success wont firing instead complete event works

I am using Angular Js with JQuery in a noodles way. 我正在以面条的方式将Angular Js与JQuery一起使用。 See my code below. 请参阅下面的代码。

Code

app.controller('ClassController', function ($scope) {
    $scope.ShowHideNoRecords = false;
    var credentials = new Object();
    credentials.SourceName = "###";
    credentials.SourcePassword = "###";
    credentials.UserName = "###";
    credentials.UserPassword = "##";
    credentials.SiteId = [-99];
    var locationIds = [1];
    var startDate = Date.today();
    var endDate = startDate;
    var dto = { credentials: credentials, startDate: startDate, endDate: endDate, locationId: locationIds };
    $.ajax({
        type: "POST",
        url: 'MbApiConnector.asmx/GetAllClasses',
        data: JSON.stringify(dto),
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        success: function (response) {
            alert(response.d);
        },
        complete: function (msg) {
            $scope.$apply(function () {
                $scope.Classes = JSON.parse(JSON.parse(msg.responseText).d);
                if ($scope.Classes.length > 0) {
                    $scope.checkin = function (id) {
                        dto = { credentials: credentials, classId: id };
                        $.ajax({
                            type: "POST",
                            url: 'MbApiConnector.asmx/Checkin',
                            data: JSON.stringify(dto),
                            contentType: "application/json; charset=utf-8",
                            dataType: "jsonp",
                            complete: function (msg) {
                                alert(msg.responseText);
                            }
                        });
                    }
                }
                else {
                    $scope.ShowHideNoRecords = true;
                }
            });
        }

    });
});

Everything is working fine with this code. 这段代码一切正常。 I knew its a bad idea mixing the two but my app was already developed in Jquery Ajax and we are upgrading with Angular JS but with lesser changes. 我知道将两者混为一谈是一个坏主意,但我的应用程序已经在Jquery Ajax中开发,并且我们正在使用Angular JS进行升级,但更改较少。 So I came up with this solution. 所以我想出了这个解决方案。 Anyways, my issues is that jquery ajax success function is not get called. 无论如何,我的问题是没有调用jquery ajax成功函数。 I am able to receive data from the webservice , but inside the complete method, as you can see in the code above. 我可以从webservice接收数据,但是可以在完整的方法内部,如上面的代码所示。

Can you explain me why its behaving so? 您能解释一下为什么它会这样吗?

May be Jquery fails to parse it as the result may not be in JSON format, try to find the error using error callback function. 可能是Jquery无法解析它,因为结果可能不是JSON格式,请尝试使用错误回调函数查找错误。 You could try with dataType : 'json'. 您可以尝试使用dataType:'json'。 error: function (err) { alert(err) } 错误:函数(err){警报(err)}

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

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