0

I'm a first time AJAX user, and I am currently helping build out a directory with user profiles. Specifically with the profile pages, I am struggling to populate the page with the JSON data being pulling via AJAX.

I have successfully been able to populate the pages from the JSON file, within the AJAX call itself. However, most of my code that builds and populates the page, is written within the AJAX success call. Unfortunately, due to the amount of info on each profile, this has led to a long page load time.

I am trying to determine the best way to adapt my code, so that only the immediately needed information (Name, Title, email, phone, Profile Picture, Education, etc) appears more quickly, with the ability to eventually access the more data heavy objects (Publications, Interests, Presentations, etc), that have many datapoints, with multiple keys to each datapoint. (For example, Publications will/could have a Title, Subtitle, Date, Publisher, etc.), with the profiles having multiple Publications.

I've attempted to make a Global Variable but things still aren't working quite right. Here's an example of the JSON Data that I'm accessing.

{
  "Error": false,
  "Cached": false,
  "Profile": {
    "FirstName": "John",
    "LastName": "Smith",
    "PreferredName": "",
    "Email": "JohnSmith@example.com",
    "Gender": "",
    "PhoneNumber": "1234567890",
    "Office": "Todd Hall Addition 570B",
    "EndowedPosition": "MAJOR POSITION #1",
    "Biography": "THIS IS A BIOGRAPHY",
    "Appointments": [
      {
        "Title": "Title 1",
        "WorkingTitle": "",
        "Rank": "",
        "Department": "Department1",
        "Start": "2014-01-01",
        "End": ""
      },
      {
        "Title": "",
        "WorkingTitle": "",
        "Rank": "Rank 1",
        "Department": "Department2",
        "Start": "2014-01-01",
        "End": ""
      }
    ],
    "IntellectualContributions": [
      {
        "Type": "Book",
        "TypeOther": "",
        "IncludeProfile": "Yes",
        "Status": "Published",
        "Title": "TitleMain",
        "TitleSecondary": "",
        "FirstName": null,
        "MiddleName": null,
        "LastName": null,
        "Role": null,
        "JournalName": "",
        "Publisher": "",
        "Volume": "",
        "IssueNum": "",
        "PageNum": "",
        "WebAddress": "",
        "CaseNum": "",
        "YearPub": "",
        "Authors": []
      },
      {
        "Type": "Journal Article",
        "TypeOther": "",
        "IncludeProfile": "Yes",
        "Status": "Published",
        "Title": "TitleMain",
        "TitleSecondary": "",
        "FirstName": null,
        "MiddleName": null,
        "LastName": null,
        "Role": null,
        "JournalName": "",
        "Publisher": "",
        "Volume": "",
        "IssueNum": "",
        "PageNum": "",
        "WebAddress": "",
        "CaseNum": "",
        "YearPub": "",
        "Authors": []
      },
      {
        "Type": "Manuscript",
        "TypeOther": "",
        "IncludeProfile": "Yes",
        "Status": "Published",
        "Title": "TITLEMAIN",
        "TitleSecondary": "",
        "FirstName": null,
        "MiddleName": null,
        "LastName": null,
        "Role": null,
        "JournalName": "",
        "Publisher": "",
        "Volume": "",
        "IssueNum": "",
        "PageNum": "",
        "WebAddress": "",
        "CaseNum": "",
        "YearPub": "",
        "Authors": []
      },
      {
        "Type": "Book Review",
        "TypeOther": "",
        "IncludeProfile": "Yes",
        "Status": "Published",
        "Title": "Title",
        "TitleSecondary": "",
        "FirstName": null,
        "MiddleName": null,
        "LastName": null,
        "Role": null,
        "JournalName": "",
        "Publisher": "",
        "Volume": "",
        "IssueNum": "",
        "PageNum": "",
        "WebAddress": "",
        "CaseNum": "",
        "YearPub": "",
        "Authors": ["Author1"]
      },
      {
        "Type": "Newspaper",
        "TypeOther": "",
        "IncludeProfile": "Yes",
        "Status": "Published",
        "Title": "Title",
        "TitleSecondary": "",
        "FirstName": null,
        "MiddleName": null,
        "LastName": null,
        "Role": null,
        "JournalName": "",
        "Publisher": "",
        "Volume": "",
        "IssueNum": "",
        "PageNum": "",
        "WebAddress": "",
        "CaseNum": "",
        "YearPub": "",
        "Authors": [
          "Author 1",
          "Author 2",
          "Author 3",
          "Author 4"
        ]
      }
    ],
    "Education": [
      {
        "DegreeType": "PhD",
        "DegreeOther": "",
        "School": "University",
        "Major": "MAJOR 1",
        "FocusArea": "Focus Area",
        "DissertationTitle": "Dissertation Title",
        "Highest": "Yes"
      },
      {
        "DegreeType": "MBA",
        "DegreeOther": "",
        "School": "University",
        "Major": "MAJOR 1",
        "FocusArea": "",
        "DissertationTitle": "",
        "Highest": ""
      },
      {
        "DegreeType": "BBA",
        "DegreeOther": "",
        "School": "University",
        "Major": "MAJOR 1",
        "FocusArea": "",
        "DissertationTitle": "",
        "Highest": ""
      }
    ],
    "Present": [
      {
        "Title": "Presentation Title1",
        "Type": "Oral Presentation",
        "Name": "NAME",
        "Authors": [
          "Author1",
          "Author2",
          "Author3",
          "Author4"
        ],
        "Date": "2014"
      },
      {
        "Title": "Presentation Title2",
        "Type": "Oral Presentation",
        "Name": "NAME",
        "Authors": [
          "Author1",
          "Author2",
          "Author3"
        ],
        "Date": "2014"
      }
    ],
    "Departments": [
      "Department1",
      "Department2"
    ],
    "Research": [
      "ResearchInterest1",
      "ResearchInterest2",
      "ResearchInterest3",
      "ResearchInterest4",
      "ResearchInterest5"
    ],
    "Teaching": [
      "TeachingInterest1",
      "TeachingInterest2",
      "TeachingInterest3",
      "TeachingInterest4"
    ]
  }
}

And here is the bit of JS/jQuery that I've got.

$.PageData = {
    FirstName: '',
    LastName: '',
    PreferredName: '',
    Email: '',
    PhoneNumber: '',
    Office: '',
    EndowedPosition: '',
    Appointments: [],
    IntellectualContributions: [],
    Education: [],
    Publications: [],
    Departments: '',
    ResearchInterests: '',
    TeachingInterests: ''
}

$(window).load(function () {
    var querystring = window.location.search;
    var username = querystring.substring(1, querystring.length).split("=")[1];
    username = username.indexOf("@") == -1 ? username : username.split("@")[0];
    $.ajax({
     type: 'GET',
     url: 'SomeURL',
     data: { id: username},
     datatype: "json",
     success: function(response){
      if (response.Error){
       alert(response.ErrorMessage);
      }else{
       if(response.Profile.length == 0){
        //No Data in Report
       }else{

                    $.PageData.FirstName = (response.Profile.FirstName);
                    $.PageData.LastName = (response.Profile.LastName);
                    $.PageData.PreferredName = (response.Profile.PreferredName);

                    $.PageData.Email = (response.Profile.Email);
                    $.PageData.PhoneNumber = (response.Profile.PhoneNumber);

                    $.PageData.Office = (response.Profile.Office);

                    $.PageData.EndowedPosition = (response.Profile.EndowedPosition);

                    $.PageData.Biography = (response.Profile.Biography);
                    $.PageData.Appointments = (response.Profile.Appointments);

                    $.PageData.IntellectualContributions = (response.Profile.IntellectualContributions);

                    $.PageData.Education = (response.Profile.Education);

                    $.PageData.Presentations = (response.Profile.Present);

                    $.PageData.Departments = (response.Profile.Departments);

                    $.PageData.ResearchInterests = (response.Profile.Research);
                    $.PageData.TeachingInterests = (response.Profile.Teaching);
                }
            }
        }
    });
});

if(($.PageData.PreferredName == "") || ($.PageData.PreferredName == null)){
    $("#Name").text($.PageData.FirstName + " " + $.PageData.LastName);
    document.title=($.PageData.FirstName + " " + $.PageData.LastName + " | Profile");
    $("#BioPageTitle").text($.PageData.FirstName + " " + $.PageData.LastName);
}else{
    $("#Name").text($.PageData.PreferredName + " " + $.PageData.LastName);
    document.title=($.PageData.PreferredName + " " + $.PageData.LastName + " | Profile");
    $("#BioPageTitle").text($.PageData.PreferredName + " " + $.PageData.LastName);
}

Any help or direction would be much appreciated!

Thanks in advance!

J.Hope
  • 27
  • 4
  • So the problem is the data takes too long to be displayed? If you want to immediately show the necessary data, maybe you should immediately return those while the actual page is loading and not include those on your ajax calls. – Bk Santiago Sep 04 '17 at 01:07
  • Welcome to Asynchronous calls.... – epascarello Sep 04 '17 at 02:35

0 Answers0