TW3JSONP |
Top Previous Next |
Where the class TW3HttpRequest is more akin to an ordinary http client component under delphi or freepascal, it does come with some limitations. First and foremost the same origin policy and also the fact that it only deals with raw, unformatted data. The alternative is the TW3JSONP class which is stored in the same unit (w3inet). It is very important to understand that these two classes utilize wildly different web technologies, and you simply cant switch one for the other. At least not without some control over the responding server. SmartCL.Inet TW3JSONP = class Methods: public Properties: property Allocated: Boolean read FAllocated;
Where TW3HttpRequest issues a raw http get or post command to your server (or a server that allow cross domain communication), TW3JSONP is all about webservices. So it expects the URL to be a program that executes on the server – and returns data in a very special way: namely as an executable script. TW3JSONP is actually a bit of a hack (like most javascript leaps of evolution are). What it does is this:
When the server is called, it parses the parameters it has received and generates not data, but rather, a javascript file which will inject the data into your webpage. When the script has been downloaded it will execute, build whatever data structure it needs to build – and then call your event handler with the result. If that sounds very dangerous then yes, we agree, you should never call a webservice you do not trust, quite simply because you have no control over what the script you get back contains. How to use it The class that ships with smart is very easy to use. All in all you have to do only 3 things:
procedure TMainForm.HandleClick(Sender:TObject); The above example uses a relatively new pascal feature called anonymous procedures, so it might look a bit alien if you are used to classical event handlers, but at least it shows you how one could go about using it. The procedure above is an event handler for a button click, so add a button to a form and connect the HandleClick to the onClick event. Processing the data The data you get back from the above webservice, is a pretty dense array of records. Thankfully smart makes it childs play to work directly with untyped javascript data. But before we can actually work with it – we have to see it. If you have chrome or safari installed on your PC, then run the example there. And remember to activate the developer tools (see tools menu in either browser). I added the writeln call for a reason, namely to push the data into the console window of the debugger — hence you can inspect it: Easy enough to work with
Now that we know how the data inside the variant we got back from the server is organized, extracting the values is easy enough. Drop a memo component on your form and alter the JSONP event handler to:
procedure TMainForm.HandleClick(Sender: TObject); And voila – run the test again, click the button, and your memo now contains the headline of the first story: Working with javascript data is very easy with smart |