简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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