By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,779 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,779 IT Pros & Developers. It's quick & easy.

inline function with ajax to set a property

P: n/a
I'm creating a simple AJAX library. It's an object that will return an
array containing the response text or xml. I'm trying to find a way to
assign the response as a property of the object, but from within an
inline function.

Within the AJAX object:
this.xmlhttp = new XMLHttpRequest();
this.response = ''; //to contain the response text OR xml

var that = this; //since we cannot reference this within the
//following inline function

this.xmlhttp.onreadystatechange = function() {
if (that.xmlhttp.readyState==4) {
// if "OK"
if (that.xmlhttp.status==200) {
if(that.xmlhttp.responseXML)
that.response = that.xmlhttp.responseText;
else
that.response = that.xmlhttp.responseText;
}
else {
alert("Problem retrieving XML data")
}
}
};

The problem is that the response isn't getting set within the Ajax
object (by copying the object reference). I'm trying to figure out a
way to set this property into the Ajax object. However, not much
experience in inline functions. Thanks,

-Eric

Nov 2 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
bobzimuta wrote:
I'm creating a simple AJAX library. It's an object that
will return an array containing the response text or xml.
I'm trying to find a way to assign the response as a
property of the object, but from within an inline function.
An 'inline' function is not usual terminology. You either mean a
function expression or an inner function. Assuming the code below is
wrapped in a function then the onreadystatechance handler is both a
function expression and an inner function.
Within the AJAX object:
this.xmlhttp = new XMLHttpRequest();
this.response = ''; //to contain the response text OR xml

var that = this; //since we cannot reference this within the
//following inline function
I would not use 'that' as the identifier for the object itself, but
'that' is not a keyword so should not actually be a problem.
this.xmlhttp.onreadystatechange = function() {
if (that.xmlhttp.readyState==4) {
// if "OK"
if (that.xmlhttp.status==200) {
if(that.xmlhttp.responseXML)
that.response = that.xmlhttp.responseText;
else
that.response = that.xmlhttp.responseText;
I cannot see much point in branching based on the true-ness of
that.xmlhttp.responseXML and then assigning that.xmlhttp.responseText in
both branches.
}
else {
alert("Problem retrieving XML data")
}
}
};
That should work, assuming the surrounding code is as I would expect
(and that the order of calling open, assigning the handler and calling
send, is correct).
The problem is that the response isn't getting set within
the Ajax object (by copying the object reference).
If the object you are referring to here is the responseXML then the fact
that you never assign it to the response may explain that (also, the
availability of responseXML depends quite a lot on the incoming XML
being correctly packaged).
I'm trying to figure out a
way to set this property into the Ajax object.
You might need to provide a wider context because (apart from as noted)
the above should do what it was coded to do.
However, not much
experience in inline functions. Thanks,


<URL: http://www.crockford.com/javascript/private.html >
<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

Richard.
Nov 2 '05 #2

P: n/a
Hello Eric,

Sorry for contacting you on an unsolicited basis, but I've read a few
of your posts on
comp.lang.javascript. I'm a journalist with Application Development
Trends (ADT, http://www.adtmag.com) and I'm working on a story on AJAX.
(Will wonders never cease, eh?)

If you don't mind, I'd like to ask you a few questions about your use
of/interest in AJAX. If you would be willing to answer a few questions
via e-mail, I'd very much appreciate it. I've appended some questions
below, and I'd ask that you answer as many (or as few) as you wish.
Don't worry about answering all of them (although, hey, that would be
fantastic :) -- just tackle the ones about which you have the most to
say.

In addition, I can quote you any way you like, so if your employer
doesn't want its name used in print, I'll be more than happy to
obfuscate its identity. Or your own, too, if you would prefer.

Thanks very much. Appreciate your time and trouble, and I apologize,
again, for contacting you on an unsolicited basis.

Best,

Steve

1. If you're working with AJAX, what kinds of (business) problems are
you using the AJAX vision/concept to try to solve? What other
products/technologies/methods have you explored, and why or why do you
not think AJAX' asynchronous JavaScript-based model is a potential
solution?

2. AJAX isn't the first spin on asynchronous remote scripting. If I
recall correctly, Microsoft introduced Remote Scripting several years
ago, after all. But Microsoft's implementation was plagued by security
problems. (I can't be bothered to look -- not right now, at least --
but I believe I can remember at least one Remote Scripting-specific
exploit for Windows NT 4.0 or Windows 2000.) As far as security is
concerned, what makes AJAX any different?

3. The "X" in AJAX stands for XML. As I understand it, however, you
don't necessarily have to use XML to "do" what is called AJAX. (AJAX,
as I understand it, describes the concept of having JavaScript
communicate directly with a server without first reloading HTML.) At
the same time, one of the most popular ways of "doing" AJAX seems to be
with the XMLHttpRequest, which -- as the "X" in AJAX indicates -- seems
to have become synonymous with the technology. So...do most of the AJAX
solutions you're aware of/have worked with use XML (or XMLHttpRequest)?
Are you aware of any approaches to doing AJAX that don't involve XML?
Can you think of any benefits or drawbacks associated with either
approach? (For example, why is the (non-standard) XMLHttpRequest one of
the most popular ways of doing what is called AJAX?)

4. Similarly, isn't there a sense in which smart developers have been
using AJAX-like approaches since before AJAX was explicitly identified?
Haven't programmers who care about application performance issues
(e.g., load times, quality-of-experience for end users) long been
"circum-coding," as it were, the inherent limitations of the
conventional HTTP client request model? If so, what approaches have
they used? Why is or why isn't AJAX any better?

5. There's a sense in which XML could actually be a hindrance -- if,
for example, you're using XMLHttpRequest to send large amounts of data
between a JavaScript client and server. Is this a potential limiting
issue for the AJAX vision, or is it largely a *non* issue (apps that
need to exchange large amounts of data by definition won't do so using
JavaScript, but -- instead -- by some other, more robust means)? I'm
leaning toward the latter understanding, mainly because AJAX seems to
describe a way to exchange *new*, *changed* or otherwise *requested*
data (i.e., rather than reloading the whole page, the HTTPD just sends
the data that the client requests.)

6. AFAIK, XMLHttpRequest isn't currently standardized, even though it's
supported by all major browsers. There are other concerns, too, in that
AJAX methods don't always conform to expected browser behaviors. (e.g.,
WikiPedia says "it might easily break the expected behavior of the
browser's back button.") Are thesee potential issues? Are they mostly
overblown?

7. Not sure if you've already addressed this issue in your
pros-or-cons-of-AJAX responses, but I've read some folks in different
newsgroups who say that the AJAX vision does away with some of the
biggest pain points associated with Java applets, namely, performance
issues and UI kludginess. (As one poster put it, because AJAX basically
consists of HTML, JavaScript, and CSS, "you can resize windows and
expect to see text and tables wrapped and presented in all the normal
ways.") Is the UI kludginess of Java aplets a significant issue, from
your perspective, or have you and other developers largely licked this
problem?

8. I wanted to ask some questions about AJAX and Microsoft's .NET
development universe. I'm aware of several thriving AJAX for .NET
solutions/sites/communities, but I'm wondering if the inimical
opposition of J2EE and .NET often precludes the use or adoption of
AJAX-like approaches in the Microsoft universe? Is AJAX such a good
idea that it's also taking root (or will take root) among .NET
developers, too? Or -- perhaps more to the point -- has JavaScript
become such a generic technology that it more or less transcends the
divisions of J2EE vs. .NET? And does Microsoft have an AJAX-like (but
..NET-centric) technology vision of its own?

Nov 23 '05 #3

P: n/a
"daedalus" <st************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

[snip]
6. AFAIK, XMLHttpRequest isn't currently standardized


Correct; however: http://www.w3.org/2006/webapi/

[snip]
Nov 23 '05 #4

P: n/a
daedalus said the following on 11/18/2005 4:54 PM:
Hello Eric,

Sorry for contacting you on an unsolicited basis, but I've read a few
of your posts on
comp.lang.javascript. I'm a journalist with Application Development
Trends (ADT, http://www.adtmag.com) and I'm working on a story on AJAX.
(Will wonders never cease, eh?)


Who is Eric?
And, why don't you email him instead? (Whoever he is).
The groups FAQ is listed below, it should provide some good reading.

P.S. This "AJAX" stuff is way over-rated.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.