{"id":494,"date":"2009-07-27T18:45:43","date_gmt":"2009-07-27T23:45:43","guid":{"rendered":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=494"},"modified":"2019-12-06T19:27:47","modified_gmt":"2019-12-07T00:27:47","slug":"how-to-create-a-json-web-service-in-asp-net","status":"publish","type":"post","link":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=494","title":{"rendered":"How To Create A JSON Web Service In ASP.NET"},"content":{"rendered":"<p>ASP.NET makes it easy to create web services but they usually return XML. Like many web developers I now prefer JSON. This article and sample code will show you how to get your web service to return data in the JSON format. The web service is written in C#. Since my web hosting company only provides me with a MySQL database, we&#8217;ll make this project a little more challenging by pulling data from a MySQL database. Finally, I will show you how to use jQuery to call your web service. So you get three kinds of value from this article; a web service in C# to return JSON data, how to use a MySQL database, and how to call an ASP.NET web service using jQuery. None of those three tasks are very intuitive or simple when using ASP.NET because they all go against the framework conventions.<\/p>\n<p>The first task to tackle is connecting to the MySQL database. ASP.NET is usually used with SQL Server or SQL Server Express so using MySQL is not always easy. On my local development web server I downloaded the <strong>MySQL Connector\/ODBC 5.1<\/strong> from <a title=\"http:\/\/dev.mysql.com\/downloads\/connector\/odbc\/5.1.html\" href=\"http:\/\/dev.mysql.com\/downloads\/connector\/odbc\/5.1.html\">http:\/\/dev.mysql.com\/downloads\/connector\/odbc\/5.1.html<\/a>. Then my web.config file merely needed a connection string node:<\/p>\n<div style=\"line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;\">\n<div style=\"line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">connectionStrings<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   2:<\/span>         <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">add<\/span> <span style=\"color: #ff0000;\">name<\/span><span style=\"color: #0000ff;\">=\"Books\"<\/span> <span style=\"color: #ff0000;\">connectionString<\/span><span style=\"color: #0000ff;\">=\"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=books;uid=root;pwd=password;option=3;\"<\/span> <span style=\"color: #ff0000;\">providerName<\/span><span style=\"color: #0000ff;\">=\"System.Data.Odbc\"<\/span><span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   3:<\/span> <span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">connectionStrings<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<\/div>\n<\/div>\n<p>NOTE: This is not my real password and there is no external access to MySQL server running on my development server.<\/p>\n<p>For this demonstration, I am using a simple database I use to keep track of the books I read. It has a single table &#8220;reading&#8221;\u009d which has only three columns; BookNum, Title, Author. I don&#8217;t know if CrystalTech has the MySQL ODBC driver installed, but I was able to create a datasource name using the control panel so the connection string used a &#8220;dsn=books&#8221; instead of the driver.<\/p>\n<p>The C# code for the web service at <a title=\"http:\/\/www.williamsportwebdeveloper.com\/BookServices.asmx\" href=\"http:\/\/www.williamsportwebdeveloper.com\/BookServices.asmx\">http:\/\/www.williamsportwebdeveloper.com\/BookServices.asmx<\/a> shows you how an ODBC connection is used to query the MySQL database. What you should notice in the code is that various method attributes are used on the web service class and methods. These method attributes require the <em>System.Web.Script<\/em> reference. If you get an error concerning missing assemblies then make sure your web.config file has everything it needs for an AJAX enabled ASP.NET site. As you can see, there is a ScriptMethod method attribute that specifies JSON as the response format. The tricky part is converting a dataset into a JSON string. I used a jagged array to accomplish that because it will serialize properly. That is probably the most valuable tip in this entire article.<\/p>\n<div style=\"line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;\">\n<div style=\"line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">using<\/span> System;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   2:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web;<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   3:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Collections;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   4:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Services;<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   5:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Services.Protocols;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   6:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Data;<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   7:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Data.Odbc;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   8:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Script.Serialization;<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   9:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Script.Services;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  10:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  11:<\/span> <span style=\"color: #008000;\">\/\/\/ &lt;summary&gt;<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  12:<\/span> <span style=\"color: #008000;\">\/\/\/ Web services to query the book database. All methods return JSON data.<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  13:<\/span> <span style=\"color: #008000;\">\/\/\/ &lt;\/summary&gt;<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  14:<\/span> [WebService(Description = <span style=\"color: #006080;\">\"Web services to query the book database.\"<\/span>, Namespace = <span style=\"color: #006080;\">\"http:\/\/www.williamsportwebdeveloper.com\/\"<\/span>)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  15:<\/span> [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  16:<\/span> [ScriptService]<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  17:<\/span> <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> BookServices : System.Web.Services.WebService {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  18:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  19:<\/span>     <span style=\"color: #0000ff;\">public<\/span> BookServices () {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  20:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  21:<\/span>         <span style=\"color: #008000;\">\/\/Uncomment the following line if using designed components <\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  22:<\/span>         <span style=\"color: #008000;\">\/\/InitializeComponent(); <\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  23:<\/span>     }<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  24:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  25:<\/span>     [WebMethod(Description = <span style=\"color: #006080;\">\"Gets the books matching part of a title.\"<\/span>)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  26:<\/span>     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  27:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">string<\/span> GetBooksByTitle(<span style=\"color: #0000ff;\">string<\/span> strTitle) {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  28:<\/span>         OdbcConnection objConnection = <span style=\"color: #0000ff;\">new<\/span> OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings[<span style=\"color: #006080;\">\"Books\"<\/span>].ConnectionString);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  29:<\/span>         OdbcCommand objCommand = <span style=\"color: #0000ff;\">new<\/span> OdbcCommand(<span style=\"color: #006080;\">\"SELECT * FROM reading WHERE Title LIKE '%\"<\/span> + strTitle + <span style=\"color: #006080;\">\"%' ORDER BY BookNum;\"<\/span>, objConnection);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  30:<\/span>         DataSet objDataSet = <span style=\"color: #0000ff;\">new<\/span> DataSet();<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  31:<\/span>         OdbcDataAdapter objDataAdapter = <span style=\"color: #0000ff;\">new<\/span> OdbcDataAdapter(objCommand);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  32:<\/span>         objDataAdapter.Fill(objDataSet, <span style=\"color: #006080;\">\"reading\"<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  33:<\/span>         objConnection.Close();<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  34:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  35:<\/span>         <span style=\"color: #008000;\">\/\/ Create a multidimensional jagged array<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  36:<\/span>         <span style=\"color: #0000ff;\">string<\/span>[][] JaggedArray = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #0000ff;\">string<\/span>[objDataSet.Tables[0].Rows.Count][];<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  37:<\/span>         <span style=\"color: #0000ff;\">int<\/span> i = 0;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  38:<\/span>         <span style=\"color: #0000ff;\">foreach<\/span> (DataRow rs <span style=\"color: #0000ff;\">in<\/span> objDataSet.Tables[0].Rows)<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  39:<\/span>         {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  40:<\/span>             JaggedArray[i] = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #0000ff;\">string<\/span>[] { rs[<span style=\"color: #006080;\">\"BookNum\"<\/span>].ToString(), rs[<span style=\"color: #006080;\">\"Title\"<\/span>].ToString(), rs[<span style=\"color: #006080;\">\"Author\"<\/span>].ToString() };<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  41:<\/span>             i = i + 1;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  42:<\/span>         }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  43:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  44:<\/span>         <span style=\"color: #008000;\">\/\/ Return JSON data<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  45:<\/span>         JavaScriptSerializer js = <span style=\"color: #0000ff;\">new<\/span> JavaScriptSerializer();<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  46:<\/span>         <span style=\"color: #0000ff;\">string<\/span> strJSON = js.Serialize(JaggedArray);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  47:<\/span>         <span style=\"color: #0000ff;\">return<\/span> strJSON;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  48:<\/span>     }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  49:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  50:<\/span>     [WebMethod(Description = <span style=\"color: #006080;\">\"Gets the books matching part of an author's name.\"<\/span>)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  51:<\/span>     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  52:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">string<\/span> GetBooksByAuthor(<span style=\"color: #0000ff;\">string<\/span> strAuthor)<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  53:<\/span>     {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  54:<\/span>         OdbcConnection objConnection = <span style=\"color: #0000ff;\">new<\/span> OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings[<span style=\"color: #006080;\">\"Books\"<\/span>].ConnectionString);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  55:<\/span>         OdbcCommand objCommand = <span style=\"color: #0000ff;\">new<\/span> OdbcCommand(<span style=\"color: #006080;\">\"SELECT * FROM reading WHERE Author LIKE '%\"<\/span> + strAuthor + <span style=\"color: #006080;\">\"%' ORDER BY BookNum;\"<\/span>, objConnection);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  56:<\/span>         DataSet objDataSet = <span style=\"color: #0000ff;\">new<\/span> DataSet();<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  57:<\/span>         OdbcDataAdapter objDataAdapter = <span style=\"color: #0000ff;\">new<\/span> OdbcDataAdapter(objCommand);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  58:<\/span>         objDataAdapter.Fill(objDataSet, <span style=\"color: #006080;\">\"reading\"<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  59:<\/span>         objConnection.Close();<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  60:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  61:<\/span>         <span style=\"color: #008000;\">\/\/ Create a multidimensional jagged array<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  62:<\/span>         <span style=\"color: #0000ff;\">string<\/span>[][] JaggedArray = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #0000ff;\">string<\/span>[objDataSet.Tables[0].Rows.Count][];<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  63:<\/span>         <span style=\"color: #0000ff;\">int<\/span> i = 0;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  64:<\/span>         <span style=\"color: #0000ff;\">foreach<\/span> (DataRow rs <span style=\"color: #0000ff;\">in<\/span> objDataSet.Tables[0].Rows)<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  65:<\/span>         {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  66:<\/span>             JaggedArray[i] = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #0000ff;\">string<\/span>[] { rs[<span style=\"color: #006080;\">\"BookNum\"<\/span>].ToString(), rs[<span style=\"color: #006080;\">\"Title\"<\/span>].ToString(), rs[<span style=\"color: #006080;\">\"Author\"<\/span>].ToString() };<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  67:<\/span>             i = i + 1;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  68:<\/span>         }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  69:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  70:<\/span>         <span style=\"color: #008000;\">\/\/ Return JSON data<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  71:<\/span>         JavaScriptSerializer js = <span style=\"color: #0000ff;\">new<\/span> JavaScriptSerializer();<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  72:<\/span>         <span style=\"color: #0000ff;\">string<\/span> strJSON = js.Serialize(JaggedArray);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  73:<\/span>         <span style=\"color: #0000ff;\">return<\/span> strJSON;<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  74:<\/span>     }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  75:<\/span><\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  76:<\/span> }<\/pre>\n<\/div>\n<\/div>\n<p>At this point you have a web service that queries a MySQL database and returns records as JSON data. This is extremely useful if you want to use that data on your home page without making it an ASP.NET page. You can get the data using AJAX and avoid having your home page show an error should your ASP.NET web application develop a problem. Next I will show you the JavaScript code for calling the web service. I&#8217;ll use the <a href=\"http:\/\/jquery.com\/\">jQuery<\/a> library because it is extremely popular with web developers right now. It makes it easy to write JavaScript that works in both browsers but you may not know how to call an ASP.NET web service using jQuery, especially since we need to pass a parameter to the web service methods. This is also tricky because the content type must be JSON.<\/p>\n<div style=\"line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;\">\n<div style=\"line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   1:<\/span> $(document).ready(<span style=\"color: #0000ff;\">function<\/span>() {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   2:<\/span>     $(<span style=\"color: #006080;\">\"#btnTitleQuery\"<\/span>).bind(<span style=\"color: #006080;\">\"click\"<\/span>, <span style=\"color: #0000ff;\">function<\/span>() {<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   3:<\/span>         $(<span style=\"color: #006080;\">\"#query_results\"<\/span>).empty();<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   4:<\/span>         $(<span style=\"color: #006080;\">\"#query_results\"<\/span>).append(<span style=\"color: #006080;\">'&lt;table id=\"ResultsTable\" class=\"BooksTable\"&gt;&lt;tr&gt;&lt;th&gt;BookNum&lt;\/th&gt;&lt;th&gt;Title&lt;\/th&gt;&lt;th&gt;Author&lt;\/th&gt;&lt;\/tr&gt;'<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   5:<\/span>         $.ajax({<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   6:<\/span>             type: <span style=\"color: #006080;\">\"POST\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   7:<\/span>             contentType: <span style=\"color: #006080;\">\"application\/json; charset=utf-8\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   8:<\/span>             url: <span style=\"color: #006080;\">\"BookServices.asmx\/GetBooksByTitle\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   9:<\/span>             data: <span style=\"color: #006080;\">'{ strTitle: \"'<\/span> + $(<span style=\"color: #006080;\">\"#txtTitle\"<\/span>).val() + <span style=\"color: #006080;\">'\" }'<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  10:<\/span>             dataType: <span style=\"color: #006080;\">\"json\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  11:<\/span>             success: <span style=\"color: #0000ff;\">function<\/span>(msg) {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  12:<\/span>                 <span style=\"color: #0000ff;\">var<\/span> c = eval(msg.d);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  13:<\/span>                 <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">var<\/span> i <span style=\"color: #0000ff;\">in<\/span> c) {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  14:<\/span>                     $(<span style=\"color: #006080;\">\"#ResultsTable tr:last\"<\/span>).after(<span style=\"color: #006080;\">\"&lt;tr&gt;&lt;td&gt;\"<\/span> + c[i][0] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;td&gt;\"<\/span> + c[i][1] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;td&gt;\"<\/span> + c[i][2] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;\/tr&gt;\"<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  15:<\/span>                 }<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  16:<\/span>             }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  17:<\/span>         });<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  18:<\/span>     })<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  19:<\/span>     $(<span style=\"color: #006080;\">\"#btnAuthorQuery\"<\/span>).bind(<span style=\"color: #006080;\">\"click\"<\/span>, <span style=\"color: #0000ff;\">function<\/span>() {<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  20:<\/span>         $(<span style=\"color: #006080;\">\"#query_results\"<\/span>).empty();<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  21:<\/span>         $(<span style=\"color: #006080;\">\"#query_results\"<\/span>).append(<span style=\"color: #006080;\">'&lt;table id=\"ResultsTable\" class=\"BooksTable\"&gt;&lt;tr&gt;&lt;th&gt;BookNum&lt;\/th&gt;&lt;th&gt;Title&lt;\/th&gt;&lt;th&gt;Author&lt;\/th&gt;&lt;\/tr&gt;'<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  22:<\/span>         $.ajax({<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  23:<\/span>             type: <span style=\"color: #006080;\">\"POST\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  24:<\/span>             contentType: <span style=\"color: #006080;\">\"application\/json; charset=utf-8\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  25:<\/span>             url: <span style=\"color: #006080;\">\"BookServices.asmx\/GetBooksByAuthor\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  26:<\/span>             data: <span style=\"color: #006080;\">'{ strAuthor: \"'<\/span> + $(<span style=\"color: #006080;\">\"#txtAuthor\"<\/span>).val() + <span style=\"color: #006080;\">'\" }'<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  27:<\/span>             dataType: <span style=\"color: #006080;\">\"json\"<\/span>,<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  28:<\/span>             success: <span style=\"color: #0000ff;\">function<\/span>(msg) {<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  29:<\/span>                 <span style=\"color: #0000ff;\">var<\/span> c = eval(msg.d);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  30:<\/span>                 <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">var<\/span> i <span style=\"color: #0000ff;\">in<\/span> c) {<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  31:<\/span>                     $(<span style=\"color: #006080;\">\"#ResultsTable tr:last\"<\/span>).after(<span style=\"color: #006080;\">\"&lt;tr&gt;&lt;td&gt;\"<\/span> + c[i][0] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;td&gt;\"<\/span> + c[i][1] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;td&gt;\"<\/span> + c[i][2] + <span style=\"color: #006080;\">\"&lt;\/td&gt;&lt;\/tr&gt;\"<\/span>);<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  32:<\/span>                 }<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  33:<\/span>             }<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  34:<\/span>         });<\/pre>\n<pre style=\"line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  35:<\/span>     })<\/pre>\n<pre style=\"line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  36:<\/span> });<\/pre>\n<\/div>\n<\/div>\n<p>You can try a working example at: <a title=\"http:\/\/www.williamsportwebdeveloper.com\/BookQuery.html\" href=\"http:\/\/www.williamsportwebdeveloper.com\/BookQuery.html\">http:\/\/www.williamsportwebdeveloper.com\/BookQuery.html<\/a>. This JavaScript is binding function prototypes to the button click events. These functions call the web service methods using the POST method and pass the required parameters as JSON data. The JSON data that it receives in response is used to create a table. You can continue to issue queries without reloading the web page. If you view the source of the <strong>BookQuery.html<\/strong> page you will find all the code required for the client page because it does not require any server side code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET makes it easy to create web services but they usually return XML. Like many web developers I now prefer JSON. This article and sample code will show you how to get your web service to return data in the &hellip; <a href=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=494\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,112,12,11],"tags":[1143,359,28,42,1149,360],"_links":{"self":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/494"}],"collection":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":5,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":3212,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/3212"}],"wp:attachment":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}