I have a server side file that returns this data
{
success: true,
m_sName:'smith, james',
m_ShortName:'Jim_S',
m_FirstName:'james',
m_LastName:'smith',
m_Rank:'CONT',
m_Unit:'',
m_Address:'PO Box 241',
m_City:'guthrie',
m_State:'KY',
m_Zip:'42234',
m_Phone:'270-956-3174',
m_Fax:'',
m_Email:'krullwarking@yahoo.com',
m_Title:'',
m_RegnetName:'james.andrew.smith',
m_BirthMonth:'10',
m_UserID:105745,
m_Age:47
}
Then on the client I have code that looks like this:
var Person = {
//Private members
getXmlRequestObj : function() {
if (window.XMLHttpRequest) {
importHTML2: new XMLHttpRequest();
} else if(window.ActiveXObject) {
importHTML2: new ActiveXObject("Microsoft.XMLHTTP");
} else {
importHTML2: "Your Browser needs an upgrade";
}
},
//Set up the global content retrieval object
//called importHTML
importHTML: new ActiveXObject("Microsoft.XMLHTTP"),
getAjaxData:function(sUrl) {
if (Person.importHTML.readyState == 4 || Person.importHTML.readyState == 0) {
Person.importHTML.open("GET", sUrl, false);// make it wait for the response
Person.importHTML.onreadystatechange = Person.handleDataArrived;
Person.importHTML.send(null);
}
},
//Called when the AJAX response is returned from any operation
//that lists handleContentArrived as its onreadystatechanged event
//for importHTML eg:importHTML.onreadystatechange = handleContentArrived; .
handleDataArrived: function() {
if (Person.importHTML.readyState == 4) {
Person._bar = eval("("+Person.importHTML.responseText+")");
Person._sName = Person._bar.m_sName;
Person._ShortName = Person._bar.m_ShortName;
Person._firstName = Person._bar.m_FirstName;
Person._lastName = Person._bar.m_LastName;
Person._Unit = Person._bar.m_Unit;
Person._securityGroups = Person._bar.m_Rank;
Person._height = '6.0 ft';
Person._weight = '280lbs';
Person._hairColor = 'Brown/gray';
Person._photoURL = 'www.photourl.com';
Person._facebookID = 'Krullwarking@yahoo.com';
Person._emailAddress = Person._bar.m_Email;
Person._phoneNumber = Person._bar.m_Phone;
Person._Fax = Person._bar.m_Fax;
Person._NetworkName = Person._bar.m_RegnetName;
Person._ID = Person._bar.m_UserID;
Person._Address = Person._bar.m_Address;
Person._City = Person._bar.m_City;
Person._State = Person._bar.m_State;
Person._Zip = Person._bar.m_Zip;
Person._Title = Person._bar.m_Title;
}
},
wholeName : function()
{
return this._firstName + ', ' + this._lastName;
},
// constructor
loadPerson : function(ID){
Person._ID = ID;
Person.getAjaxData('getUser.asp?uid='+ID);
},
setFirstName : function(fname) {
Person._firstName = fname;
return true;
},
// add the methods to the prototype so that all of the
// Foo instances can access the private static
getFirstName : function() {
return Person._firstName;
}
}
Student = {
superClass:Person,
sayHello : function(){ alert('hi, I am a student, and my name is '+this.superClass.wholeName());}
}
Student.superClass.loadPerson(105745);
Student.sayHello();
This works fine but only in IE because of the activex reference to xmlHTTP. everytime I try to make the importHTML variable call the this.getXmlRequestObj or Person.getXmlRequestObj it doesnt work…..How can I do this?
Your problem is that you never use
getXmlRequestObj. I’m guessing you first tried to do something likeBut that doesn’t work, because you can’t refer to an object’s properties in the object’s own object literal definition. Instead, you can create a one-time initialization function that sets up
importHTML, which you call immediately after the object definition.EDIT:
Your JSON does not have quoted keys. JavaScript is perfectly happy to have objects like this:
but the JSON specification is more strict. It requires object keys to be quoted, like so:
evalexecutes JavaScript, so it accepts the looser syntax.JSON.parseand most other JSON utilities will only accept the strict syntax.