Com And Hosting

In this  post I will try to demonstrate how to create a webservice for API to post and retrieve data using AJAX. I am going to use c# and Visual Web Developer 2010 for this example. You can download Visual Studio Express 2013 for Web from Microsoft website. Application Programming Interface (API) is very handy tool in modern web technology to sync data back and forth between multiple systems. It is quite secure and using AJAX also made it work seamlessly without interfering user activity. It is being commonly used in many mobile devices to retrieve information from central database.

First lets add a webservice file to the website. Right click on the website in solution explorer window then click on “Add new item”.

solution_explorer_1

From the list of files select the language you want to use. In this case I have selected Visual C# to write my webservice. Let’s give it a name “user_favourites.asmx”. Basically I want to retrieve user favourites from the MySql server and this API will be used in iPhone App to display user favourites. I have selected the option “place code in the separate file”. This will create the code file in ASPNET folder called APP_CODE.
solution_explorer_2

It should also open the file for editing, once created. Lets add some references in the header section of the page to use in the events.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using MySql.Data.MySqlClient;
using System.IO;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using System.Web.Security;

I have added bunch of common library to connect to MySql database, produce json data and use user login.

Leave a Reply

Your email address will not be published.