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 7897171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:05:39+00:00 2026-06-03T08:05:39+00:00

I have the below code to create a class in javascript…. Now i will

  • 0

I have the below code to create a class in javascript…. Now i will only pass the ID of the ‘website’ and then via an ajax call i will get the rest of the information from the database (JSON ENCODED).

Now my only problem is that when i see whats in the object at the end its only showing the id.

The ajax calls works fine because if i alert this.address after success (ajax) its displaying the result.

What I presume is that I cannot set a property with an ajax request… Can you help?

function website(id) {
     this.id = id; //id


     $.ajax({  //website_information
      type: "GET",
      url: '/proc.php?proc=website_name&id=' + this.id + '',
      success: function(data){
      var tmp = $.parseJSON(data);
     this.address = tmp.website_address;

     this.name = tmp.website_name;
      }
    });

    }

    var obj = new website('20');
    obj.alertwebsite();
    console.log(obj);
  • 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-03T08:05:40+00:00Added an answer on June 3, 2026 at 8:05 am

    There are two problems here. The first is that $.ajax is asynchronous. That means it returns before the request is complete. The success function is run when the request is complete, but obj.alertwebsite() will be run before.

    The second problem is the value of this within an AJAX callback. Within the callback, this is set to an object containing all the settings for the AJAX call. This means you are setting address and name properties on this object. There are two ways around this. The first is that = this as in other answers. The nicer way is to use the context setting in the AJAX call:

    function website(id) {
        this.id = id; //id
    
        $.ajax({ //website_information
            type: "GET",
            url: '/proc.php?proc=website_name&id=' + this.id + '',
            context: this,
            success: function (data) {
                var tmp = $.parseJSON(data);
                this.address = tmp.website_address;
    
                this.name = tmp.website_name;
            }
        });
    }
    

    This allows you to customise what this means inside the callback. This is documented in the jQuery AJAX documentation.

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

Sidebar

Related Questions

I have a dialog box, created with the code javascript and HTML generated below.
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below XAML code : <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml DataContext={Binding RelativeSource={RelativeSource Self}} WindowStartupLocation=CenterScreen
How can i implement the below code with jQuery/AJAX timer,(I have created 2 URL
I'm using the prototype javascript libraries. My code is below. I've created a class
In my code I have 2 ComboBox DropDownLists, created by the code below. The
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have below code to insert a style into DOM (there is a use
I have below code.The logic here is if HostList conatains any blanck entry it
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)

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.