Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:44:16+00:00 2026-06-07T15:44:16+00:00

I have a webpage which when it is loaded makes an ajax request to

  • 0

I have a webpage which when it is loaded makes an ajax request to get some data in JSON [] format from the back end and display it on the page. The webpage is generated with MVC .NET

The function that loads the events from the back end:

function initEvents() {
    $.ajax({
        url: '/Work/GetEvents/' + id, type: "GET", dataType: 'json',
        success: function (data) {
            events = data;
        },
        error: function(data) {
            //  Note: hardcoded JSON event in here
            events = hardcodedEvents;
        },
        complete: function(data) {
            // Add events
            for (var i = 0; i < events.length; i++){
                addEvent(events[i]);
            }
        }
    });
}

When I make the request fail (ie change the url to point to bogus data) – it goes in error: function(data) – so the hardcoded JSON object loads fine then it goes in the ‘complete: function(data)` and adds the events 1 by 1.

However, when I use the correct URL and get the events from the backend, it seems the events arrive too late; just as it exits the complete section. The question is: Why is this happening? I thought complete loaded whenever the reply came back. Am I wrong to believe this? How can I fix this so the events are loaded from the back end and into the page?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:44:17+00:00Added an answer on June 7, 2026 at 3:44 pm

    events is not defined in the scope of the complete event. You should pass the events data to another function, or simply move the addEvent logic to within the event handler. eg:

    function onEventData(data) {
        for (var i = 0; i < data.length; i++){
            addEvent(data[i]);
        }
    }
    
    function initEvents() {
        $.ajax({
            url: '/Work/GetEvents/' + id, type: "GET", dataType: 'json',
            success: onEventData, 
            error: function(data) {
                //  Note: hardcoded JSON event in here
                onEventData(hardcodedEvents);
            }
        });
    }
    

    Note that complete is called when the request finishes (after success and error callbacks are executed). Since you only want to run your handler once per request, you shouldn’t use it in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an webpage which posts some data and then redirects to a page
I have created a Webpage which will post as post method..not as get method.
I have content on a webpage which is both sent from the server at
I have a webpage, on which I am using Ajax to replace different divs
I have an ASP.Net 4.0 web application which very frequently loads data from the
I have a webpage with a combo box on the page which is loaded
I have a main window (#1) on my webpage from which I open a
Here's the situation I have a webpage which has one drop down called prefer.
I have a WebPage on which I have to detect and prevent usage of
I have an image on a webpage which is being dynamically generated by a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.