Thursday 19 August 2010

WCF DataService and jQuery (Part 2 The Revenge)

Continuing from where I left off yesterday...

The WCF DataService is up and running and accessible via IIS on http://localhost. Now I can write a simple web page that uses jQuery to talk to the service and read some data. There are loads of other blog posts showing you how to do this; I based my solution on something that Shawn Wildermuth posted here: http://wildermuth.com/2010/02/23/WCF_Data_Services_and_jQuery.

Unfortunately I can't post the HTML because Blogger won't allow it and I haven't got time to set up my own domain with some blogging software that's more suited to a developer. But you can use the example I linked to above and figure it out. It's worth noting that Microsoft typically complicates things by adding a wrapper around the JSON that you get back so you have to do results.d.FieldName to get to the data.

What gets even crazier is that if I switch the GET to POST and add a data field to the $.ajax function (something like data: '{"CategoryName":"Test Category"}') then I can create a new record in the database without having to write a stored procedure or a SQL statement or anything. Pretty incredible.

No comments: