Datasheet
description: YAHOO.buzzWatch.editInPlace.get(‘textDescription’)
}
};
var o = YAHOO.util.Connect.getConnectionObject();
o.conn.open(“POST”, “watch.php”, true);
YAHOO.util.Connect.initHeader(‘Content-Type’,’application/xml’);
YAHOO.util.Connect.setHeader(o);
YAHOO.util.Connect.handleReadyState(o, this.saveCallback);
o.conn.send(xotree.writeXML(tree));
}
This method uses the JKL.ParseXML library to create a XML document from a JavaScript object: the tree
object is a standard JavaScript object containing the values that will constitute the XML document and the
conversion itself is done by the instruction
xotree.writeXML(tree). Chapter 12 covers in detail the dif-
ferent ways to exchange XML over HTTP. All you need to know for the moment is that, like programming
objects, HTTP resources have a number of methods attached to them and that each method corresponds to
a different action that is specified by the HTTP specification. Up to now, BuzzWatch had used only the
GET
method, but to save watches, you will use a POST method (that was clearly visible in the server’s log).
Posting XML documents with this version of the YUI is slightly more verbose than getting one and requires
six statements. The first one (
var o = YAHOO.util.Connect.getConnectionObject();) gets a new
connection object. The second one opens the connection to the server. The third and fourth ones initialize
and set the header that describes the media type. The fifth one sets up the callback handler, and the last one
sends the request together with the XML document. The request sent by the browser is:
POST /buzzwatch/watch.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.8.0.4) Gecko/20060608
Ubuntu/0.9.3 (Ubuntu) StumbleUpon/1.909 Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;
q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/xml
Content-Length: 181
Pragma: no-cache
Cache-Control: no-cache
<?xml version=”1.0” encoding=”UTF-8” ?>
<watch>
<symbol>msft</symbol>
<tag>microsoft</tag>
<title>Microsoft</title>
<description>The company we love to hate.</description>
</watch>
Server side, it fires the same watch.php script that you’ve already seen but reaches a branch of the main
if/then/else test that you’ve not explored yet:
if (strlen($HTTP_RAW_POST_DATA)>0) {
write();
}
21
Hello Web 2.0 World
04_087889 ch01.qxp 5/2/07 12:56 PM Page 21