Two Laptop Bag*
The moose likes HTML, CSS and JavaScript and the fly likes XMLHtmlRequest return Undefine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "XMLHtmlRequest return Undefine" Watch "XMLHtmlRequest return Undefine" New topic
Author

XMLHtmlRequest return Undefine

Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Hi,

I am trying to POST XML data to the external website using FireFox. Post version for IE works fine, but when I use XMLHtmlRequest to post the data in the FireFox I get undefined


var oHTTP = new XMLHttpRequest();
alert("oHTTP" oHTTP.value);

alert display's undefine.

JavaScript ---------------




[ July 14, 2008: Message edited by: Bear Bibeault ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I have never heard about the value property of a XMLHttpRequest object. Can I ask where you got that information from.

Eric
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Hi,

As far as I know XMLHtmlRequest is used to send the request from the JavaScript to the external website. I have a XML data which need to be send to the external website for processing for IE browser I have used ActiveX object
var oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
var oXML = new ActiveXObject("Microsoft.XMLDOM");
which don't work in FireFox, that the reason I am using
var oHTTP = new XMLHttpRequest();

Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
There is no oHTTP.value.

Here is an entry level tutorial on the subject.

Eric
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Thanks Eric,

Then How I am suppose to handle open("Post",url,sync) to post the XML string to the external url in Firefox to process it. and secondly if you can suggest the below equivalent code for FireFox that will be Great as this below Javascript works fine in IE and not in FireFox.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Instead of dealing with all this browser-specific mess, why not just employ a JavaScript library such as Prototype or jQuery that handles all of that for you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Where I can fine this library's and how can I use then do you have any examples which I can refer. I need to fine the solution at the earliest, did couple of search and using XMLHttpRequest was the best available and simple option to implement.
But i am still confuse why doesn't XMLHttpRequest works. I am intiallizing it and then to it return undefined?

Thanks,
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Here is the link i did not post: http://developer.apple.com/internet/webcontent/xmlhttpreq.html

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

You'll find Prototype here, and jQuery here. There are a lot of other libraries, by the way, but these are the two that I think are the easiest to adopt and are the least invasive.

I usually recommend jQuery as the easiest to adopt quickly.

As an example, to load an HTML fragment from a URL into a div named targetDiv is as easy as:

That's it. And all browser-weirdnesses are handled.
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Hi Eric,

I have used the same structure only difference it rather then using the GET i am using the POST but it doesn't seems to be working. Any suggestion.
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Hi Bear,

Thanks for you suggestion will I be able to post the XML data with Jquery ? Just want to be sure before I start it implementation
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Yes. For that, you would use one of the more general methods such as $.get() or $.post().
[ July 14, 2008: Message edited by: Bear Bibeault ]
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Hi Bear,

Do I need to create a handler for to use jquery? since if I use $.post("sada.com") in my JavaScript it throws and Error. I have download the Jquery and is include in my JSP.
Any help is appreciated.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What is the error message?

Are you trying to post to a different domain than the one you are on?

Eric
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
yes that right eric I am trying to post to different domain. Undefined is the error.when i used XMLHttpRequest handler to post the Sting to the External URL
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Never say "I got an error"

Say "Here is the error that I got:" and include the full and accurate text of the error (and any other information that goes with it).

Otherwise, we can only guess.
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Here is the entire code Bear, and Error is "Undefined"



Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The XMLHttpRequest object can not post to a different domain. JavaScript has the same orgin policy meaning you are stuck with only your domain. If you want to talk to another domain you need to either look into a JSON request or set up a serverside proxy.

Eric
Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Hi,

only one common problem i found with your code
send current time to server as i have mentioned, you will not face this problem any more.And this will also help you to work on different browsers.


Thank You.
Have a Nice Time.

-Santosh


var xhReqs = null;
function createXMLHttpRequest() {
if (typeof XMLHttpRequest == 'undefined') {
// IE
try {
xhReqs = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhReqs = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhReqs = new XMLHttpRequest();
}
}

function responseXmlParserVote(operation,id) {

if (xhReqs == null) {
createXMLHttpRequest();
}
if (xhReqs.readyState != 0) {
xhReqs.abort();
}
var url;

url= "<%=request.getContextPath()%>/action.do?operation=" + operation+"&id="+id+"&";


var d = new Date();
var ctime = d.getTime();

url += "&time="+ctime;

xhReqs.open("POST", url, true);

xhReqs.onreadystatechange=function() {
if (xhReqs.readyState == 4) {
var root = xhReqs.responseXML.documentElement;

};
xhReqs.send(null);
}
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
SantoshkumarJeevan Pawar:

"POST" requests do not get cached so adding a timestamp is not going to make a difference in this case. For you information: there is a better way of handling the cache and there is no timestamp needed.

<blockquote>code:
<pre name="code" class="core">
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
</pre>
</blockquote>

Eric
Manu Garg
Greenhorn

Joined: May 29, 2008
Posts: 17
Thanks SantoshkumarJeevan Pawar this doesn't seems to be working too. I need the XML string to be posted to the external URL.

Bear, Can you provide some input on the JQuery you have mentioned ?

Thanks,
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I feel like a broken record. The XMLHttpRequest object can not talk to an outside domain. You would have to use a serverside proxy to talk to another domain.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XMLHtmlRequest return Undefine
 
Similar Threads
Values from serialize are all null
Alert Box