使用jQuery痛苦地减慢ajax调用

时间:2016-02-02 13:28:56

标签: javascript jquery ajax knockout.js

我在我的应用程序中使用knockout来注册/从表单登录但是ajax调用的等待时间第一次非常缓慢(之后猜测它的缓存,因为它' ;第二次非常快#轮次) - 当我在线上传网站时,大约需要15秒才能登录,当我将其作为iOS应用程序(HTML5应用程序)打包时,它需要超过几秒钟才能完成登录。为什么会发生这种情况?我错过了什么吗?它更可能是服务器端吗?希望我能提供足够的信息,但不幸的是我对此不熟悉。我将在下面添加登录代码:

    $(document).ready(function(){

    function UserViewModel() {

        //Make the self as 'this' reference
        var self = this;

       var Domain = "http://example.com";

        //Declare User observables which will be bind with UI
        self.UserId = ko.observable();
        self.Name = ko.observable();
        self.Email = ko.observable();
        self.Occupation = ko.observable();
        self.Country = ko.observable();
        self.RegistrationNumber = ko.observable();

        //Create User object
        var User = {
            UserId: self.UserId,
            Name: self.Name,
            Email: self.Email,
            Occupation: self.Occupation,
            Country: self.Country,
            RegistrationNumber: self.RegistrationNumber,
        };

        //Assign knockout observables to User/s objects
        self.User = ko.observable();  //user
        self.Users = ko.observableArray(); // list of users

        //onload set status of user
        UserStatus();

        //Login handler
        self.login = function () {

            try {

                if (User.Email() != "" && User.RegistrationNumber() != "") {

                    //try logging in
                    Login();

                } else {
                    viewModel.UserId("Please login with the correct email and registration number.");
                }
            }

            catch (err) {
                viewModel.UserId("There was an error, please try again.");
            }

        };

    //Login
        function Login() {


            $.ajax({
                   url: Domain + '/User/Login',
                   cache: false,
                   type: 'POST',
                   dataType: 'json',
                   contentType: 'application/json; charset=utf-8',
                   data: '{"Email":"' + User.Email() + '","RegistrationNumber":"' + User.RegistrationNumber() + '"}',
                   beforeSend: function () {
                       // setting a timeout
                     $('.splash').show();

                   },
                   success: function (data) {
                   $('.splash').hide();
                   if (data != 0) {

                   SetUserVars(data.UserId, data.Name, data.Email, data.Occupation, data.Country, data.RegistrationNumber);

                   viewModel.UserId(ActionToTake());
                   }
                   else {
                   viewModel.UserId("The supplied credentials are invalid, please try again.");
                   }
                   },
                   complete: function () {
                    //$('.splash').hide(); 
                   },
                   }).fail(
                           function (xhr, textStatus, err) {
                           console.log(xhr.statusText);
                           console.log(textStatus);
                           console.log(err);
                           viewModel.UserId("There was an error, please try again.");
                           });
        }

function UserStatus() {

        if (localStorage.getItem("UserId") === null) {

            //not logged in
            $("a.menu-status").text("Login").attr("href", "index.html#login-screen");

        }
        if (localStorage.getItem("UserId") != null) {
            //logged in
            $("a.menu-status").text("Logout").attr("href", "index.html#login-screen");



        }

        //allow user to logout and reset all user storage
        $("a.menu-status").click(function () {

            //show logged off status
            $("a.menu-status").text("Login");

            alert('You have logged off, please login if you wish to continue.');

            self.reset();

            //redirect

            window.location.replace("index.html#login-screen");
            location.reload();
            viewModel.UserId("You have logged off.");

            ResetUserLocalStorage();

        });

    }

1 个答案:

答案 0 :(得分:0)

我倾向于同意问题在于服务器端而非客户端的评论。

最初采用的步骤是使用postman https://www.getpostman.com/之类的东西并通过它点击API,验证它的缓慢部分。

如果显示问题,那么您是否可以使用服务器上运行的代码调试自己?然后逐步完成代码,并尝试确切地指出发生了什么以及它在哪里放慢速度。