Returning multiple values using XMLHTTP object?

N

Noozer

Hrm.. last posting was mangled. Let's try again, with more detail...

I'm just starting to try out "Ajax" web programming and I've got a question.

AJAX is fairly straightforward. Javascript creates an XMLHttp object and
then uses that to generate a hidden webpage with your results.

What I'd like to know is, how can I retrieve multiple values from an XMLHttp
request?For example, if my request generated an SQL query that returned a
Name, Address and PhoneNumber, could I get those values directly instead of
parsing them out of the ResponseText?

This is my current handler:

function showResult(pge) {
var url="getResult.asp?sid=" + Math.random() + "&q=" + pge
xmlHttp=GetXmlHttpObject(stateChanged)
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
}

function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
//Complete, so show results.
document.getElementById("myResult").innerHTML=xmlHttp.responseText;
//How to get mulitple values here?
}
}
 
L

Luke Matuszewski

Noozer napisal(a):
function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
//Complete, so show results.
document.getElementById("myResult").innerHTML=xmlHttp.responseText;
//How to get mulitple values here?
}
}

Response form of request made via XmlHttpObject can be anything. Here
you can do:

- generate XML Document on the server side and send it as response to
the XmlHttpObject using proper Content-type header value (either
text/xml, application/xml, or ends in +xml), than use responseXML;

- use JSON data interchange format, create desired structure on server
side and send it as response to the XmlHttpObject (here as
Content-type header value i used text/plain), than use
var myJSONdata = eval(xmlHttp.responseText);

It is good to set also Cache-Control to "no-cache" header, but
generally idea used in AjaxRequest library works in real world (adding
unique URIComponent to query string, search thru google for this free
library).

, also you can do:

var myJSONdata = JSON.parse(xmlHttp.responseText);

, using JSON implementation by Douglas Crockford (which i also have
used with success, search thru google).

Hope it helps.

Best regards
Luke M.
 
N

Noozer

Luke Matuszewski said:
Noozer napisal(a):

Response form of request made via XmlHttpObject can be anything. Here
you can do:

- generate XML Document on the server side and send it as response to
the XmlHttpObject using proper Content-type header value (either
text/xml, application/xml, or ends in +xml), than use responseXML;

Since I'm generating the data using ASP, I think that XML is the way to go.
Unfortunately I can't figure out how to do this and I'm having no luck
finding examples online.
- use JSON data interchange format, create desired structure on server
side and send it as response to the XmlHttpObject (here as
Content-type header value i used text/plain), than use
var myJSONdata = eval(xmlHttp.responseText);

JSON seems like a good idea, but again, not finding any easily understood
examples of it's usage.
var myJSONdata = JSON.parse(xmlHttp.responseText);

, using JSON implementation by Douglas Crockford (which i also have
used with success, search thru google).

Will take a look.
Hope it helps.

Definately!

Thanks!
 
L

Luke Matuszewski

Noozer said:
JSON seems like a good idea, but again, not finding any easily understood
examples of it's usage.

If you are using ASP and .NET Framework, than this will probably be the
best:

http://jayrock.berlios.de/ (if you read this site carefully, you will
see also examples).

People writing here in this group helped me a lot, so now i am helping
others.

Best regards !
Luke M.

PS i have used JSON without any example, but using the JSON
implementation for Java and JavaScript provided by Douglas Crockford.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top