Jump to content

Archived

This topic is now archived and is closed to further replies.

jcombs_31

AJAX returned data

Recommended Posts

Can I use ajax to get an array of data and parse it with javascript for my result.  I've just scratched the surface of ajax and know how to return a single value or string, but I need to get multiple fields from a database and parse it for the inner html.

Share this post


Link to post
Share on other sites
Can you splice the string together send it and then separate it in Javascript?  maybe splice it together with commas, then cut it apart again at the commas?  Then you'd be doing something you know how to do plus something that's relatively easy to do.

Share this post


Link to post
Share on other sites
Yea, I thought about that, does javascript have a string tokenizer?

edit: nevermind, got it with split.

Share this post


Link to post
Share on other sites
Use XmlHttpRequest.responseXML instead of .responseText.

The server should then ofcourse return well-formed xml.

Simple tut: http://www.ajaxtutorial.net/index.php/2006/02/28/ajax-with-php-using-responsexml/

Share this post


Link to post
Share on other sites
I've been toying with ajax with responseXML myself today.

It took me a quite a while to figure out, so I thought I'd be so kind to to share it with ya'll.  :)

You already know how to create a request object, so I won't go into that.

First off, you need to make sure that the server will return a valid xml document, headers and all:

[code]<?php
//Generate the contents of $updates here

header('Content-Type: text/xml');
header('Cache-Control: max-age=0, must-revalidate'); //No caching of ajax content right now :)
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<response>';
foreach($updates as $id=>$content) {
echo '<update xml:id="'.$id.'">
<![CDATA['.$content.']]>
</update>';
}
echo '</response>';
?>[/code]
Note the CDATA section.
CDATA nodeType isn't in the W3C recommendation, it's a Microsoft creation. CDATA is not supposed to be parsed. Anyway, in FF it's accessible by 'DOMNode.textContent'.

You've got to love it.

Anyway, here's my - not so sophistocated (didn't spell that right, did I?) but powerfull enough for right now - response handler:

[code]function responseHandler() {
if(http.readyState == 4 && http.status == 200) {
var response = http.responseXML;
if(response) {
var updates = response.getElementsByTagName('update');
for(i=0; i<updates.length; i++) {
//Test to see if .textContent holds anything:
if(updates.item(i).textContent == null) {

//Nope. Probably on IE. Try for CDATA nodeValue.
var html = updates.item(i).firstChild.nodeValue;
if(!html) {
//Whaaaat? Still nothing? Nevermind then.
return;
}
}
else {
//Aaaah. Thank you.
var html = updates.item(i).textContent;
}
document.getElementById(updates.item(i).getAttribute('xml:id')+'container').innerHTML = html;
}
}
}
} [/code]

Works like a charm. Now you can get anything you can imagine from the server, including whole chunks of html!

I'm exited, are you?  ;D

Share this post


Link to post
Share on other sites
It's easier to separate our different informations via " | " and then use split :)

And also there is some problem ith XML when you search in web JSON is used more then XML, they say it's more secure. But still I use responseText :)

Share this post


Link to post
Share on other sites
[quote author=radalin link=topic=103020.msg412299#msg412299 date=1155113414]
It's easier to separate our different informations via " | " and then use split :)
[/quote]
No it's not. Show me how you use split to differentiate between different TYPES of information, and I'll show you 2k examples that are either impossible or require a lot more coding than using responseXML.

[quote author=radalin link=topic=103020.msg412299#msg412299 date=1155113414]
And also there is some problem ith XML when you search in web JSON is used more then XML, they say it's more secure. But still I use responseText :)
[/quote]

There is some problem with XML? How very descriptive.  :P JSON seems nice, but I'll stick with XML for now.

Share this post


Link to post
Share on other sites
Using the responseXML property can be useful for data (usually a larger amount than when using responseText) that needs to maintain its structure. You could always get the swiss army knife out on the string, but it's can be less efficient programming. In many cases, using responseXML is just more readable as well.

448191, it'd save you some typing if you stopped using the item method. Concerning the updates var you used in your loop, you could've just used [b]updates[ i ][/b] rather than [b]updates.item(i)[/b]. The latter method is deprecated, if I'm not mistaken. It's just my style to save space, especially in JavaScript, so feel free to disregard. ;)

Share this post


Link to post
Share on other sites
[quote author=448191 link=topic=103020.msg412524#msg412524 date=1155138807]
No it's not. Show me how you use split to differentiate between different TYPES of information, and I'll show you 2k examples that are either impossible or require a lot more coding than using responseXML.
[/quote]
You are possibly right about that one but still it's easier for me. I know how I write the text data and after the split I know that first one is id and second one is name etc... Yes it requires more coding and more coding means more complexivity but I'm more comfortable with that one

[quote author=448191 link=topic=103020.msg412524#msg412524 date=1155138807]
There is some problem with XML? How very descriptive.  :P JSON seems nice, but I'll stick with XML for now.
[/quote]

http://siteexperts.spaces.live.com/blog/cns!CE6C50D25BFAAA73!4852.entry

I reached this article while searching how I can secure data transfer with ajax and how i can secure ajax functions callings. It's written by one of live.com's software engineers. If you have time just read it, it's very helpful and very understandble, but for those who hasn't time I just quote the paragraph here:
[quote=Scott Isaac]
    Parsing XML is Slow
    We have also learned that merely parsing the RSS XML can be expensive in the browser. When we parse RSS, we are merely translating it into Javascript structures to be further manipulated. Since our server’s are already normalizing feeds to a standard format, instead of serving the RSS feed directly, we are going to translate the feed directly to JSON (Javascript structures). As a simple benchmark, on my fast developer machine we went from 400ms to parse 150K of RSS to 15ms to “execute” the JSON response.
[/quote]
That was one of the problems I was talking about. Even if I do not like MS a lot, I appreciate the features they give with live.com. And it's good article to read anyway. (There are other comments that blame Atlas for the live.com's performance issue too which I do not have the slightest idea as I'm a PHP developer and I do not care about Atlas either but even MS thinks to pass JSON from XML why shouldn't I? But text is the best!!! :)

Share this post


Link to post
Share on other sites
[quote author=curtis link=topic=103020.msg413687#msg413687 date=1155283496]
448191, it'd save you some typing if you stopped using the item method. Concerning the updates var you used in your loop, you could've just used [b]updates[ i ][/b] rather than [b]updates.item(i)[/b]. The latter method is deprecated, if I'm not mistaken. It's just my style to save space, especially in JavaScript, so feel free to disregard. ;)
[/quote]

I know I can refer to items in a nodelist like it were array items, but it makes less sense to me, as I'm not iterating an array but a nodelist. I don't mind a couple of extra chars if it makes more sense to me. Don't know if it's depreciated, I'll look that up. If it is I'll stop using it.

As for "parsing xml is slow": Compared to JSON; likely. Compared to complex string manipulation, effectively builiding your own javascript parser; NOT.

Eventually I'll look into JSON, but I've got more important things to study right now.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.