Fork AJax returning XML doc

1 view
Skip to first unread message

Mwalimu Gini

unread,
May 21, 2008, 9:23:48 AM5/21/08
to Fork JavaScript
Hi all,

Is it possible to return the ajax request in Fork ajax as an xml doc.
the we we can do normally on ajax. coz i have tried tweaking it in
vain. it just returns it as string not a xml doc that can be accessed
as a DOM. any help would be appreciated. thanx in advance.

Antoine

Peter Michaux

unread,
May 21, 2008, 11:19:25 AM5/21/08
to forkjav...@googlegroups.com
Hi Antoine,

perhaps you have something like this

on200: function(xhr) {
someFunction(xhr.responseText);
}

To access the responseXML you should be able to do

on200: function(xhr) {
someFunction(xhr.responseXML);
}

I have never used this however as I use JSON as my transport format.

Peter

Mwalimu Gini

unread,
May 22, 2008, 3:33:49 AM5/22/08
to Fork JavaScript
thanx for the reply.
i have tried that and it still doesnt work. is there anyone who has
been able to or knows how to return an xml doc with fork ajax?

Peter Michaux

unread,
May 22, 2008, 7:44:45 PM5/22/08
to forkjav...@googlegroups.com
On Thu, May 22, 2008 at 12:33 AM, Mwalimu Gini <aow...@googlemail.com> wrote:
>
> thanx for the reply.
> i have tried that and it still doesnt work. is there anyone who has
> been able to or knows how to return an xml doc with fork ajax?

The xhr object sent to the callback functions of a Fork Ajax request
is just a regular browser xhr object. It is not wrapped or anything
special. For this reason I think the question is not actually
Fork-specific but rather a general xhr/xml question. Are you sure your
xml document is truly a valid xml document?

Peter

Mwalimu Gini

unread,
May 23, 2008, 3:42:44 AM5/23/08
to Fork JavaScript
Thanks peter for the reply again.
ok, this is what is returned by the forkajax
<?xml version="1.0" encoding="ISO-8859-1"?>
<gallery>
<gxml_id>id_less</gxml_id>
<gxml_ownerid>God Father</gxml_ownerid>
<gxml_name>Antoine</gxml_name>
<gxml_share>Hustlenomics Inc.</gxml_share>
</gallery>

but when i try to access the document with the below js script my
firebug gives me the xmlDoc 'has no properties' error...
xmlDoc=o.responseXML;
document.getElementById("gala_id").innerHTML=
xmlDoc.getElementsByTagName("gxml_id")[0].childNodes[0].nodeValue;
document.getElementById("gala_name").innerHTML=
xmlDoc.getElementsByTagName("gxml_name")[0].childNodes[0].nodeValue;
document.getElementById("gala_share").selected=
xmlDoc.getElementsByTagName("gxml_share")
[0].childNodes[0].nodeValue;

bloew are the div's to be filled
<div id="gala_name">dummy</div>
<div id="gala_id">dummy</div>
<div id="gala_share">dummy</div>

what could be the problem?

Peter Michaux

unread,
May 23, 2008, 1:22:09 PM5/23/08
to forkjav...@googlegroups.com
On Fri, May 23, 2008 at 12:42 AM, Mwalimu Gini <aow...@googlemail.com> wrote:

> ok, this is what is returned by the forkajax

I think you mean returned by the server.

> <?xml version="1.0" encoding="ISO-8859-1"?>
> <gallery>
> <gxml_id>id_less</gxml_id>
> <gxml_ownerid>God Father</gxml_ownerid>
> <gxml_name>Antoine</gxml_name>
> <gxml_share>Hustlenomics Inc.</gxml_share>
> </gallery>
>
> but when i try to access the document with the below js script my
> firebug gives me the xmlDoc 'has no properties' error...
> xmlDoc=o.responseXML;
> document.getElementById("gala_id").innerHTML=
> xmlDoc.getElementsByTagName("gxml_id")[0].childNodes[0].nodeValue;
> document.getElementById("gala_name").innerHTML=
> xmlDoc.getElementsByTagName("gxml_name")[0].childNodes[0].nodeValue;
> document.getElementById("gala_share").selected=
> xmlDoc.getElementsByTagName("gxml_share")
> [0].childNodes[0].nodeValue;
>
> bloew are the div's to be filled
> <div id="gala_name">dummy</div>
> <div id="gala_id">dummy</div>
> <div id="gala_share">dummy</div>
>
> what could be the problem?

I'm not sure. I just tried an experiment successfully.

I created a new Ruby on Rails application

$ rails xmlExpt
$ cd xmlExpt
$ mongrel_rails start xmlExpt

I edited the file xmlExpt/public/index.html to be the following

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>test page</title>
<script type="text/javascript" src="javascripts/ajax.js"></script>
<script type="text/javascript">
var go = function() {
console.log("going");
new FORK.Ajax('GET', '/some.xml', {
onComplete: function(xhr) {
console.log("returned");
console.log(xhr.responseXML);
}
});
console.log("gone");
};
</script>
</head>
<body>
<p style="font-size:500%"><a href="#"
onclick="try{go();}catch(e){console.log(e);}return false;">go</a></p>
</body>
</html>

Then I added a file xmlExpt/public/some.xml with this content.

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

I pointed my browser to http://loccalhost:3000/ and pressed the big
"go" link. When I looked in the Firebug console I could see the xml
document was returned.

Just as an extra check I ran curl to make sure the content-type header
of the xml response was correct

$ curl -I http://localhost:3000/some.xml
HTTP/1.1 200 OK
Connection: close
Date: Fri, 23 May 2008 17:20:22 GMT
ETag: "4836fae7-a1-d6cdde"
Last-Modified: Fri, 23 May 2008 17:12:07 GMT
Content-Type: text/xml
Content-Length: 161

It looks good with "text/xml"

Please give this example a try as I have described.

Peter

Mwalimu Gini

unread,
May 26, 2008, 6:12:41 AM5/26/08
to Fork JavaScript
Thanks alot Peter i finally solved it. i just added a header with text/
xml as content type and wala! it worked. apparently the document being
returned was html so it wasnt recognized as a xml doc. Thanks again i
am sorted!
> I pointed my browser tohttp://loccalhost:3000/and pressed the big
> "go" link. When I looked in the Firebug console I could see the xml
> document was returned.
>
> Just as an extra check I ran curl to make sure the content-type header
> of the xml response was correct
>
> $ curl -Ihttp://localhost:3000/some.xml
Reply all
Reply to author
Forward
0 new messages