简体   繁体   English

使用jQuery痛苦地减慢ajax调用

[英]Painfully slow ajax calls using jQuery

I'm using knockout in my application to register/login from a form but the wait times on ajax calls are painfully slow first time 'round (guessing it's caching afterwards as it's really quick second time 'round) - around fifteen seconds to login when I upload the site online, and when I wrap it up as an iOS app (HTML5 application) it takes over SIXTY seconds to complete login. 我在我的应用程序中使用knockout来从一个表单注册/登录但是ajax调用的等待时间第一次'非常缓慢(猜测它之后是缓存,因为它非常快速第二次'轮次) - 大约十五秒登录时我在线上传网站,当我将其作为iOS应用程序(HTML5应用程序)打包时,它需要超过SIXTY秒才能完成登录。 Why could this be happening? 为什么会发生这种情况? Have I missed something? 我错过了什么吗? Is it more likely to be server-side? 它更可能是服务器端吗? Hopefully I can give enough info but unfortunately I'm new to this. 希望我能提供足够的信息,但不幸的是我是新手。 I'll add the Login code below: 我将在下面添加登录代码:

    $(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();

        });

    }

Id be inclined to agree with the comments that the issue lies with the server side and not the client side. 我倾向于同意问题在于服务器端而非客户端的评论。

The steps id take initially would be to use something like postman https://www.getpostman.com/ and hit the API through that, verify that its the slow part. 最初的步骤是使用像邮递员https://www.getpostman.com/这样的东西并通过它点击API,验证它的缓慢部分。

If that shows the issue then can you get yourself in a debug situation with the code thats running on the server? 如果这表明问题,那么您是否可以使用服务器上运行的代码调试自己? Then step through the code and try to pin point exactly whats happening and where its slowing down. 然后逐步完成代码,并尝试确定发生了什么,以及它在哪里放慢速度。

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

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