469,366 Members | 2,342 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

object Error

Ike

Can someone please illuminate to me why, in the following snippet of script,
the alert statement in the try-catch gives me [object Error] ? The file
'http://localhost:1222/roomx1/getdata.php' truly does exist. I'm really lost
here and would appreciate anyone's good eye. Thanks, Ike
if(typeof window.ActiveXObject != 'undefined'){
req = new ActiveXObject("Microsoft.XMLHTTP");
req.onreadystatechange=processReqChange;
}else{
req = new XMLHttpRequest();
req.onload=processReqChange;
}
try{
req.open('GET', 'http://localhost:1222/roomx1/getdata.php',true);
}catch(e){
alert(e);
}

Mar 27 '06 #1
7 21927
Several points to note:

-You should point out which browser(s) you test in

-As far as I am aware, you should be using onreadystatechange for the
XMLHttpRequest object as well

-If you were getting the <current> page from a normal http request
(http://localhost/path), note that in most implementations, you cannot
change port to something else.
In FireFox, simply re-stating the protocol and host (even unchanged)
will result in a permission denied error.
In IE, you may or may not be able to change ports, but that's a
security relaxation Microsoft likes adding to IE. It's not standard, do
not rely on it.

try also:
if(window.ActiveXObject) {} instead of your current code

Mar 27 '06 #2
Ike wrote:
Can someone please illuminate to me why, in the following snippet of
script, the alert statement in the try-catch gives me [object Error] ? The
file 'http://localhost:1222/roomx1/getdata.php' truly does exist.
There is the problem: It is not a file, it is a resource, retrieved via
HTTP. And as such, accesses to it are subject to the Same Origin Policy.
That might be relevant here, because if the protocol, second-level domain,
or port address are different from the ones of the accessing resource, it
cannot work without further privileges. Search the archives.
[...]
if(typeof window.ActiveXObject != 'undefined'){
This will be true in IE on Windows, Netscape on Windows, and Gecko-based UAs
with the ActiveX plugin.
req = new ActiveXObject("Microsoft.XMLHTTP");
This will fail in Netscape on Windows, and Gecko-based UAs with the ActiveX
plugin. You do not handle the exception, so the script will break.
req.onreadystatechange=processReqChange;
}else{
req = new XMLHttpRequest();
req.onload=processReqChange; ^^^^^^^^^^
Nonsense.
}
try{
You do exception handling in the wrong place.
req.open('GET', 'http://localhost:1222/roomx1/getdata.php',true);
}catch(e){
alert(e);
}


Should be

// global context
var _global = this;

function isMethod(a)
{
var t;
return (a && ((t = typeof a) == 'function' || t == 'object'));
}

// anywhere
var req;

if (isMethod(_global.XMLHttpRequest))
{
req = new XMLHttpRequest();
}
else if (isMethod(_global.ActiveXObject))
{
try
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
// maybe you want to try other things here
}
}

if (req)
{
// exception handling only for debugging purposes
try
{
req.open('GET', 'http://localhost:1222/roomx1/getdata.php');
req.onreadystatechange = processReqChange;
}
catch (e)
{
alert(e.name);
}
}
PointedEars
Mar 27 '06 #3
AndrewTK wrote:
Several points to note:

-You should point out which browser(s) you test in
Full ACK
try also:
if(window.ActiveXObject) {} instead of your current code


Nonsense.

BTW:

- You should quote the minimum of what you are replying to.
PointedEars
Mar 27 '06 #4
Ike
Thanks Andrew,
-If you were getting the <current> page from a normal http request
(http://localhost/path), note that in most implementations, you cannot
change port to something else.
In FireFox, simply re-stating the protocol and host (even unchanged)
will result in a permission denied error.
In IE, you may or may not be able to change ports, but that's a
security relaxation Microsoft likes adding to IE. It's not standard, do
not rely on it.


Yes, that was the problem, by-and-large. But I have other problems here, as
pointed out in the other posts in this thread. I have taken an example of
what I wish to do from w3schools, amended it, and am posting it under a
different thread with a few (new) questions regarding this, -Ike
Mar 27 '06 #5
Thank you, Thomas, for your comment to my post as well.

As a return comment to you:
if(typeof window.ActiveXObject != 'undefined')


Should never work as 'undefined' is never returned by an undefined
object. 'undefined' is a string. myvar == 'undefined' will only return
true if myvar contains the string "undefined". I should have caught
that one initially, admitted.

if(typeof window.ActiveXObject != undefined)

Should not work either, as undefined is not a value. As of recent
browser implementations, however, it does, so that would be a valid
check.
if(window.ActiveXObject) {} instead of your current code


Nonsense.

^ ^ ^ ^ ^
That's rude and uncalled for. Firstly, for its unduly dismissive
pretentiousness, secondly because you are wrong. That's always been the
way to check for undefinedness. Try:

// ===
function func(arg) {
if(arg) {alert(arg);}
else {alert("No arg passed "+arg);}
}
func("hello");
func();
// ===

I will urge you to keep future messages polite. People post here for
advice. If you disagree, say so, and say why.

Mar 27 '06 #6
AndrewTK wrote:
Thank you, Thomas, for your comment to my post as well.
It would have been appreciated more if you had learned how to quote.
Please provide attribution of quoted material next time.
As a return comment to you:
if(typeof window.ActiveXObject != 'undefined')
Should never work as 'undefined' is never returned by an undefined
object.


Nonsense.
'undefined' is a string.
And a `typeof' operation evaluates to a string. It evaluates to
"undefined" if there is no such property or the property value is
`undefined'.
myvar == 'undefined' will only return true if myvar contains the
string "undefined".
`typeof' takes precedence over `=='.
I should have caught that one initially, admitted.
Pardon?
if(typeof window.ActiveXObject != undefined)

Should not work either, as undefined is not a value.
Nonsense. It does not work because a `typeof' operation evaluates to
a string value, and `undefined' evaluates to none. `undefined' is a
property of the Global Object that has `undefined', the sole value of
the Undefined type, as its value.
As of recent browser implementations, however, it does, so that would
be a valid check.
No, it will not. And it would fail if the Global Object had no such
property, which is why I did not use it.
if(window.ActiveXObject) {} instead of your current code


Nonsense.

^ ^ ^ ^ ^
That's rude and uncalled for. Firstly, for its unduly dismissive
pretentiousness,


It is but hard fact that you are unwilling to accept. Your problem.
secondly because you are wrong. That's always been the way to check for
undefinedness.


Nonsense.

Whether it is defined or not does not matter. Whether it can be called or
not does matter. However, using a type-converting test instead of a
`typeof' test evaluates the property value and so causes a warning. Less
efficient, and unnecessary, considering that `typeof' is supported since
JavaScript 1.1, JScript 1.0, and ECMAScript Edition 1.
PointedEars
Mar 27 '06 #7
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
AndrewTK wrote:

Nonsense.

^ ^ ^ ^ ^
That's rude and uncalled for. Firstly, for its unduly dismissive
pretentiousness,


It is but hard fact that you are unwilling to accept.


The two are not mutually exclusive.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 28 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pkpatel | last post: by
2 posts views Thread by Roby Eisenbraun Martins | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.