This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XMLHttpRequest.open() Exception

 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing an application based on XMLHttpRequest in Javascript. My host is numeric-based for testing (my local, private IP), so the complete URL I'm using has the form https://aaa.bbb.ccc.ddd/members/servlet/ReqMgr, where aaa.bbb.ccc.ddd is numeric.

When I call the XMLHttpServletRequest open method, it throws an exception (the request is on the same machine as all the other pages; this is NOT a cross-domain application). However, the send seems to go ahead and work, and I actually get the correct response from the server.

But, the Javascript does not execute the onreadystate=4 code or have any sort of status. It just pops up a dialogue and asks what application I want to use to open the response. I can open it with a text editor and see the correct server response. I've tried sending it back with various Content-Type's, but don't think this is the problem.

Anyone seen anything like this?

TIA
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The content type very well may be the problem. If it's not a type that the browser understands, it may pop up the "what the heck do expect me to do with this?" dialog.

Have you tried just using "text/plain"?

Although the exception on open() is suspicious. Do you get the same chain of events across different browsers?
[ March 20, 2007: Message edited by: Bear Bibeault ]
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, tried text/plain; it just went to a browser screen with the raw text sent back from the server, like it would if I had just gone to a text file. Javascript timed out. Same as before, except instead of asking me what to use to open the response, it just displayed it as text.

The URL I'm sending in the XMLHttpRequest is of the form https://aaa.bbb.ccc.ddd/members/servlet/ReqMgr. I have also tried sending just the /members/servlet/ReqMgr part, and couldn't make that work, either, but I think it is related(?).

TIA.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allen Williams:
I have also tried sending just the /members/servlet/ReqMgr part, and couldn't make that work, either, but I think it is related(?).


I think it's the key. You shouldn't have to use the full absolute URL. For some reason the browser is confused about domains.

Why are you using IP addresses instead of real host names? Again, do differnet browsers act the same?
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Firefox; with IE (shudder), nothing happens (of course, I don't have any type of Javascript console on IE). When I make the url just /members/servlet/ReqMgr instead of https://aaa.bbb.ccc.ddd/members/servlet/ReqMgr, I get an error status 400 with the message "No Host matches server name aaa.bbb.ccc.ddd".
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're right that it's key, but I can't make it work either way. The page is actually an https: (note the 's') page, and various combinations of "http[s]://aaa.bbb.ccc.ddd/members/servlet/ReqMgr" (with & without the 's') behave differently.

I think (well, I'm no expert, this is my first attempt at an XMLHttpRequest app, but based on the books & 'net searching I've done) that you're also right: I shouldn't have to have any of the protocol:host stuff in the URL, but, in Firefox & IE both, I get that "No Host..." statusText without it.

Arrrggghhh! I've been fooling with this one problem for three days!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The browsers are very picky about this sort of thing to protect against cross-scripting attacks.

I'd suggest getting your code working in a more normal environment (no https, use localhost or other non-IP domain) to elminate coding issues.

Once you've got good and known working code, then try migrating it to the secure environment a step at a time.
[ March 20, 2007: Message edited by: Bear Bibeault ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with the XMLHttpRequest if you are in HTPPS all requests have to be HTTPS. you can not mix HTTP in there.

Eric
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I was doing my best to not mix http & https in my experimentation. While y'all were writing these responses, I modified it to try using "GET" instead of "POST", same results. I'm gonna try Bear's suggestion, and go to localhost. The problem with starting with no https is that this is sort of in the middle of a webapp that is all https.

Besides, and this is at the heart of some of my confusion, if I don't need the proto://host portion of the URL to pass to open or send (depending on whether GET or POST), I assume it's getting it from the current page (that invokes the Javascript) and it would automagically put in the right protocol.

If I don't get any results with the above experiments, I'm also going to try a synchronous request. I'll post what happens tomorrow.
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I made it localhost, forgetting that meant I couldn't test it from my office (localhost, of course, refers to whatever is this machine- doh!). So I gave my Linux machine a name in the /etc/hosts, then figured out how to do the same thing on my Windoze office machine, and, voila! The same error. Evidently giving the machine a named network address vs. a numeric IP doesn't make any difference.

Any more ideas?
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, with named host, I get exactly the same behavior regardless of which of the formats of url I use, i.e., it wants to download the JSON data. As before, when I tell it to download in a text editor, it's exactly what I expect.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Install Firefox and install Firebug (http://www.getFirebug.com)

Eric
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firefox is what I use, and I have Firebug installed, but can't get the console to work. I haven't tried to figure that out because I can just use alerts. Is there something in particular in should look for in Firebug?

I stripped the code down to what should be the bare minimum. I've included some support functions in here just for completeness, but the heavy lifting is in the last function, doHttpRequest(). I've bolded it. Note the alert in my callback. When I run it, I get, first, an onreadystate of 2, then an onready state of 4. In both cases, the status is 400 and the status message is 'No Host matches server name anw-dev' (anw-dev what I named my server). The responseText is empty and the url is /members/servlet/ReqMgr. The servlet never gets called. At the alert, it throws this exception:

Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: https://anw-dev/scripts/globals.js :: anonymous :: line 101" data: no]
Source File: https://anw-dev/scripts/globals.js
Line: 101


But I'm not sure it's relevant.



TIA
anw
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I found the problem. I'm using a fully-qualified URL of the form 'https://anw-dev/members/servlet/ReqMgr' to access the servlet with the XMLHttpRequest, but I didn't have an equivalent context path in my server.xml file for Tomcat. My context path was something like "/mywebapp", so I made my request URL "/mywebapp/members/servlet/ReqMgr" and now, at least the minimal, synchronous request works. I think I could have added a context path of "" and it would also have worked, but, as of right now, it ain't broken so I ain't gonna fix it;-).

WHEW!!! Four days later...it's always the little things.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see this for what that error means: http://radio.javaranch.com/pascarello/2006/02/07/1139345471027.html

For firebug, watch this:
http://www.yuiblog.com/blog/2007/01/26/video-hewitt-firebug/

Eric
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I read about the error & understand it, and I'll watch the Firebug video tomorrow.

I appreciate all the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic