Connecting Tech Pros Worldwide Forums | Help | Site Map

object Error

Ike
Guest
 
Posts: n/a
#1: Mar 27 '06

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);
}




AndrewTK
Guest
 
Posts: n/a
#2: Mar 27 '06

re: object Error


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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#3: Mar 27 '06

re: object Error


Ike wrote:
[color=blue]
> 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.[/color]

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.
[color=blue]
> [...]
> if(typeof window.ActiveXObject != 'undefined'){[/color]

This will be true in IE on Windows, Netscape on Windows, and Gecko-based UAs
with the ActiveX plugin.
[color=blue]
> req = new ActiveXObject("Microsoft.XMLHTTP");[/color]

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.
[color=blue]
> req.onreadystatechange=processReqChange;
> }else{
> req = new XMLHttpRequest();
> req.onload=processReqChange;[/color]
^^^^^^^^^^
Nonsense.
[color=blue]
> }
> try{[/color]

You do exception handling in the wrong place.
[color=blue]
> req.open('GET', 'http://localhost:1222/roomx1/getdata.php',true);
> }catch(e){
> alert(e);
> }[/color]

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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Mar 27 '06

re: object Error


AndrewTK wrote:
[color=blue]
> Several points to note:
>
> -You should point out which browser(s) you test in[/color]

Full ACK
[color=blue]
> try also:
> if(window.ActiveXObject) {} instead of your current code[/color]

Nonsense.

BTW:

- You should quote the minimum of what you are replying to.


PointedEars
Ike
Guest
 
Posts: n/a
#5: Mar 27 '06

re: object Error


Thanks Andrew,
[color=blue]
> -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.[/color]

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


AndrewTK
Guest
 
Posts: n/a
#6: Mar 27 '06

re: object Error


Thank you, Thomas, for your comment to my post as well.

As a return comment to you:
[color=blue]
> if(typeof window.ActiveXObject != 'undefined')[/color]

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.
[color=blue][color=green]
>> if(window.ActiveXObject) {} instead of your current code[/color]
>
> Nonsense.[/color]
^ ^ ^ ^ ^
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.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#7: Mar 27 '06

re: object Error


AndrewTK wrote:
[color=blue]
> Thank you, Thomas, for your comment to my post as well.[/color]

It would have been appreciated more if you had learned how to quote.
Please provide attribution of quoted material next time.
[color=blue]
> As a return comment to you:
>[color=green]
>> if(typeof window.ActiveXObject != 'undefined')[/color]
>
> Should never work as 'undefined' is never returned by an undefined
> object.[/color]

Nonsense.
[color=blue]
> 'undefined' is a string.[/color]

And a `typeof' operation evaluates to a string. It evaluates to
"undefined" if there is no such property or the property value is
`undefined'.
[color=blue]
> myvar == 'undefined' will only return true if myvar contains the
> string "undefined".[/color]

`typeof' takes precedence over `=='.
[color=blue]
> I should have caught that one initially, admitted.[/color]

Pardon?
[color=blue]
> if(typeof window.ActiveXObject != undefined)
>
> Should not work either, as undefined is not a value.[/color]

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.
[color=blue]
> As of recent browser implementations, however, it does, so that would
> be a valid check.[/color]

No, it will not. And it would fail if the Global Object had no such
property, which is why I did not use it.
[color=blue][color=green][color=darkred]
>>> if(window.ActiveXObject) {} instead of your current code[/color]
>>
>> Nonsense.[/color]
> ^ ^ ^ ^ ^
> That's rude and uncalled for. Firstly, for its unduly dismissive
> pretentiousness,[/color]

It is but hard fact that you are unwilling to accept. Your problem.
[color=blue]
> secondly because you are wrong. That's always been the way to check for
> undefinedness.[/color]

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
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#8: Mar 28 '06

re: object Error


Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
[color=blue]
> AndrewTK wrote:[/color]
[color=blue][color=green][color=darkred]
>>> Nonsense.[/color]
>> ^ ^ ^ ^ ^
>> That's rude and uncalled for. Firstly, for its unduly dismissive
>> pretentiousness,[/color]
>
> It is but hard fact that you are unwilling to accept.[/color]

The two are not mutually exclusive.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread