Returning JSON from Ajax-Enabled WCF Service
This is a simple way to display data from your database without calling a server side script bindings and without postbacks. This article shows you how to create a JSON enabled web service inside your ASP.net website.
· Visual Studio 2010
1. First, create a website or use a pre-existing one.
2. Add a new file and select the ‘AJAX-enabled WCF Service.
3. The IDE will generate the .svc file and the code behind of the service located in App_Code folder
4. Create a new method that you wish to return a list of data.
5. Notice the attribute of the Method GetCustomers()
You must include the WebInvoke attribute in order for the system ensures that this method returns a JSON format.
6. Open your web.config file and replace the <enableWebScript /> into <webHttp />
7. Build and try to run the service with the uri indicated on the WebInvoke attribute.
8. Now that the service displays a JSON data, it’s time to parse and display it into HTML.
9. Create a simple HTML page and create a method that will show the data from your JSON returned URL.
10. Now build the application and run the html page.
This approach is useful when it comes to simplicity and does not require extensive server side controls to display information from your database or any other data source.