/    Sign up×
Community /Pin to ProfileBookmark

learning JSON: what’s wrong with my code?

hi, learning to use JSON here, having trouble run the following snippet. What did I do wrong? FF complains “xmlhttp.open is not a function”

[code]
var readDiscussion = {
URL : function(){
var serviceURL = “”;
//preform some program and get a good url….
return serviceURL;
} , //end URL
Connect : function(){
if(XMLHttp.ajaxCall){
var xmlhttp = XMLHttp.ajaxCall;
xmlhttp.open(‘GET’, this.URL, true);
.
.
.
.
(rest of ajax code …)
.
.
.
[/code]

[code]
/*
*XMLHttp obj is responible to create ajax call and send to server
* @method
* -ajaxCall returns a valid xmphttp obj based on mozilla, safari, opera, or IE. return false otherwise
*/
var XMLHttp = {
ajaxCall : function(){
var httpRequest;
try{httpRequest = new XMLHttpRequest();}//mozilla, safari
catch(e){
try{httpRequest = new ActiveXObject(“Msxml2.XML HTTP”);} //IE
catch(ee){
try{httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);}
catch(eee){
httpRequest = false;
}//end Cannot create XMLHTTP instance
}//IE4,5
}//end IE5,6
return httpRequest;
}//end ajaxCall
}//end XMLHttp
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Jan 30.2008 — You are assigning the function to the variable, you are not executing it.

Change:

<i>
</i>var xmlhttp = XMLHttp.ajaxCall;

to
<i>
</i>var xmlhttp = XMLHttp.ajaxCall();


Eric
Copy linkTweet thisAlerts:
@sirpelidorauthorJan 30.2008 — Eric, thx for the reply.

Is it safe to assume whenever I have a method looks like this:
<i>
</i>var myObj = {
myProperty : "boo" ,
myMethod : function(){...}
}


I will always have to use the () (like myObj.myMethod() instead of myObj.myMethod)

what about regular properties?

Reason I ask is because i'm running into a similar problem:

it said:

document.getElementById(this.myProperty) has no properties
<i>
</i>var myObj = {
myProperty : "boo" ,
init : function(){
document.getElementById(this.myProperty).innerHTML = "BOO!";
}
}


nothing fancy here..
<i>
</i>&lt;html&gt;
&lt;head&gt; ... script above..&lt;/head&gt;
&lt;body&gt;&lt;div id="boo"&lt;/div&gt;&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@A1ien51Jan 30.2008 — Take a read through this: http://java.sun.com/javascript/ajaxinaction/Ajax_in_Action_ApB.html

It is part of my one book. It might help you understand some stuff a little better.

Eric
Copy linkTweet thisAlerts:
@sirpelidorauthorJan 31.2008 — Eric,

Thank you for the link, it was a good read. Now I that realize my code didn't work because I had scoping issue. Basically mis-used the "this" keyword.

From a developer stand point, when would you create objects using JSON and when would you use function?

What would consider as best practices, do you have example you can share?

Thank you.
×

Success!

Help @sirpelidor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...