responseText

H

Haris Bogdanovic

Hi.

I have a page like this:

---------------------------------------------------------------------------

<html>
<head>
<script type='text/javascript'>
function xhrFunc() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
alert(xhr.responseText);
};
return 0;
};
xhr.open('GET', 'test', true);
xhr.send(null);
return 0;
};
</script>
</head>
<html>
<p onclick='xhrFunc();'>
xhr
</p>
</html>
</html>

--------------------------------------------------------------------------

Whenever I click on "xhr" I get a messagge box with the
source code of this page.
Why the responseText property returns that instead of something from "test"
page ?

Thanks
 
B

beegee

Whenever I click on "xhr" I get a messagge box with the
source code of this page.
Why the responseText property returns that instead of something from "test"
page ?

What is "test"? Is it a page on your server? Just 'test' or
test.php, test.html, test.phtml etc? You must specify the full name
and relative path to webroot of the page unless you are using a
RESTful framework on your server. Please read up on AJAX first.

Bob
 
E

Evertjan.

Haris Bogdanovic wrote on 17 apr 2011 in comp.lang.javascript:
Hi.

I have a page like this:

-----------------------------------------------------------------------
----

<html>
<head>
<script type='text/javascript'>
function xhrFunc() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
alert(xhr.responseText);
};
return 0;
};
xhr.open('GET', 'test', true);
xhr.send(null);
return 0;
};
</script>
</head>
<html>
<p onclick='xhrFunc();'>
xhr
</p>
</html>
</html>

-----------------------------------------------------------------------
---

Whenever I click on "xhr" I get a messagge box with the
source code of this page.
Why the responseText property returns that instead of something from

Because alert() does not render html,
and object.responseText returns the response-body of object as a string.
 
V

VK

Whenever I click on "xhr" I get a messagge box with the
source code of this page.
Why the responseText property returns that instead of something from "test"
page ?

First of all, XMLHttpRequest will raise a security exception if used
in this way locally, unless it is IE and using the ActiveXObject
constructor instead of XMLHttpRequest constructor (the first has the
original more relaxed security model).

So you need to load this page, let's say it is called test.html, to
your server. Then you are issuing GET request for a bogus "test"
resource. Any properly configured server in this case raises 404 Not
Found error and sends back the default 404 error page. The content of
this page becomes the property of responseText and this is what you
see, not the page you sent request from - just take a closer look.
 
D

David Mark

First of all, XMLHttpRequest will raise a security exception if used
in this way locally, unless it is IE and using the ActiveXObject
constructor instead of XMLHttpRequest constructor (the first has the
original more relaxed security model).


Ah, some things never change. But you do have some bits and pieces of
facts mixed in with the fantasy at least.

You want to try that again veekay? Hint: IE is not alone in allowing
for XHR over the file protocol.
 
V

VK

You want to try that again veekay?  Hint: IE is not alone in allowing
for XHR over the file protocol.

I don't need to try: I did it before posting. For more information
anyone is welcome to read my "Using XMLHttpRequest run locally for
local data reading" study I posted back in 2010:
http://groups.google.com/group/comp..._frm/thread/56bcdb1f5051ef97/f984d362953ffc35
(that covers much more including different per-UA security policies
for parent and sub-directories). The specifics of this particular case
("if used in this way locally" I said) is that OP addressing a bogus
(non-existing) local file. And in this case Gecko raises security
exception from chrome. And the particularity of IE to mention
separately is that it allows to get the same object but with different
security models using ActiveXObject or
XMLHttpRequest constructors. This is a little known and badly (or not
at all) documented fact worth to mention to a novice.

P.S. How is your My Library business going? Here the rumors are that
Microsoft and IBM are moving all their web-solutions on My Library
because of its huge popularity, stability and security. Can you
confirm? And my most sincere congratulations if it's true!
 
S

S.T.

P.S. How is your My Library business going? Here the rumors are that
Microsoft and IBM are moving all their web-solutions on My Library
because of its huge popularity, stability and security. Can you
confirm?

He's added a few new approaches to promote it... "deranged
Twitter-stalker" and "deranged inappropriate Twitter-hashtagger":

http://twitter.com/Cinsoft
 
V

VK

He's added a few new approaches to promote it... "deranged
Twitter-stalker" and "deranged inappropriate Twitter-hashtagger":

"They can take everything from us but hope" (c) :)
 
T

Thomas 'PointedEars' Lahn

Andrew said:
What does one do with the posts of an anonymous poster whose only
purpose seems to be to attack the reputation of another person?

Ignore them. *Please.*


PointedEars
 
S

S.T.

What does one do with the posts of an anonymous poster whose only
purpose seems to be to attack the reputation of another person?

I'm not attacking his reputation. His reputation is firmly established -
I'm only highlighting it.

I don't understand your lap dog role either. Read his posts here. Read
his tweets. Does he strike you as someone who isn't seeking attention,
negative or otherwise? Throwing a jab or two DM's way is a gift to him.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top