PDA

View Full Version : Http GET request?


jpan
04-19-2004, 10:22 AM
Hi,

Is there a tutorial somewhere that deals with communication with a remote server somewhere through HTTP?

Specifically, I have a piece of JavaScript code:


var xmlrequest = new ActiveXObject("microsoft.xmlhttp");

xmlrequest.open("POST", requestString, false);
xmlrequest.setRequestHeader("Content-type", "text/xml");
xmlrequest.send();
return xmlrequest.responseXML.xml;


How can I achieve the above? (I believe my problem here is that there's no ActiveXObject available?)

antun
04-19-2004, 10:40 AM
See here:

http://www.laszlosystems.com/developers/learn/documentation/tutorials/data.php#http

The model is to declare a HTTP dataset that will hold the returned XML from the remote URL.

(I believe my problem here is that there's no ActiveXObject available?)

That's right. JavaScript (or ECMAScript) has been extended in its applicaitons (browsers, scripting languages etc). Laszlo is based on the ECMA-262 standard, with some differences:

http://www.laszlosystems.com/lps-2.1/docs/lzx-reference/?info-scripting.html

-Antun

jpan
04-19-2004, 11:34 AM
Hmm... ok, I tried a bunch of things, but my lack of knowledge in laszlo just isn't helping..

Here's what I want to do:
1) Compose a URL with a bunch of parameters
2) Send a HTTP POST to a remote server.
3) Get back some XML data, and populate a dataset.
4) Read the dataset and display some info (through debug, for now).

I've done this:

canvas.RemoteData.setQueryType("POST");
canvas.RemoteData.setSrc(sendto+requestString);
canvas.RemoteData.doRequest();
var dp = canvas.RemoteData.getPointer();
return dp.xpathQuery('XMLMessage/CustomerName/text()');


which doesn't work, but I'm not sure why...

The remote XML looks like

<?xml version="1.0" encoding="ISO-8859-1" ?>
<XMLMessage>
<CustomerName>John</CustomerName>
...
</XMLMessage>


I just can't find out what I'm missing here!!! Please help!!

antun
04-19-2004, 12:44 PM
There's a couple of problems:

1) Don't append the querystring to the src of the dataset. There's a setQueryString() method in dataset if you have already built up your querystring in the form: foo=bar&ta=smelly

http://www.laszlosystems.com/lps-2.1/docs/lzx-reference/?dataset.html#meth-setQueryString

2) Data requests are aysnchronous, meaning that although this bit of code is correct:


canvas.RemoteData.doRequest();
var dp = canvas.RemoteData.getPointer();
return dp.xpathQuery('XMLMessage/CustomerName/text()');


... execution of your code isn't just going to stop and wait for the response after the doRequest() call.

The standard way to have something happen when the data comes back is to declare a datapointer (in advance), with an xpath pointing into the data that will come to the new dataset, and set some code to go off when it sends its ondata event. Typically in an app you'd have onerror and ontimeout handler methods in that datapointer too.

A quicker way would be to add an ondata method to your dataset, and put this code in there:


var dp = this.getPointer();
Debug.write( dp.xpathQuery('XMLMessage/CustomerName/text()') );


-Antun

jpan
04-20-2004, 06:27 AM
I tried the quick way, and it worked great! So now I know that the data *is* coming into the dataset, and it was justa synchronization issue...

So, I want to do it the proper way, like you mentioned, having a datapointer declared ahead of time, with the XPath specified, and implement ondata, onerror, and ontimeout.


<dataset name="RemoteMessage" type="http" />
<datapointer id="dp" xpath="RemoteMessage:/">
<method event="ondata">
debug.write( "["+this.xpathQuery('XMLMessage/CustomerName/text()')+"] should be James" ); // should be James
</method>
<method event="onerror">
debug.write( "an error has occurred while loading data");
</method>
<method event="ontimeout">
debug.write( "timed out loading data");
</method>
</datapointer>


However, the ondata event is not raised when the data gets loaded into RemoteMessages... what am I doing wrong??

Thanks!

antun
04-20-2004, 09:17 AM
I think that's because your datapointer is pointing to the root of the dataset. I tried it with the weather service on the Laszlo website, and got the same result: with the datapointer set to weather_ds:/, no ondata would fire, but with it set to weather_ds:/weather, I go the expected behavior:


<canvas debug="true">
<dataset name="weather_ds" request="false"
src="http://www.laszlosystems.com/cgi-pub/weather.cgi" />

<datapointer name="weather_dp" xpath="weather_ds:/weather">
<method event="ondata">
Debug.write( "data came back" );
Debug.write( this.serialize() );
</method>
<method event="onerror">
Debug.write( "There was an error getting the error data" );
</method>
<method event="ontimeout">
Debug.write( "Request for weather data timed out" );
</method>
</datapointer>


<button>Make request for weather
<method event="onclick">
weather_ds.setQueryString( "zip=94107" );
weather_ds.doRequest();
Debug.write( "Request made" );
</method>
</button>
</canvas>


-Antun