Before you begin with the client-side code, you should have a server code that will respond to all the requests coming from your client-server code. In this case, I've created a web service to handle the requests. Notice the green comments.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Script.Serialization;
  6. using System.Web.Script.Services;
  7. using System.Web.Services;
  8. using System.Xml.Linq;
  9. namespace DataReaderService
  10. {
  11. /// <summary>
  12. /// Summary description for EmpReader
  13. /// </summary>
  14. [WebService]
  15. //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16. [System.ComponentModel.ToolboxItem(false)]
  17. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  18. [System.Web.Script.Services.ScriptService]
  19. public class EmpReader : System.Web.Services.WebService
  20. {
  21. [WebMethod]
  22. [ScriptMethod(ResponseFormat = ResponseFormat.Json) ] //format the returned string value as JSON string
  23. public string getEmpData(string id)
  24. {
  25. //Create a list of 'employee' object
  26. List<employee> emps = new List<employee>();
  27. //Load the XML document
  28. var employee = XElement.Load( Server.MapPath("Employees.xml") );
  29. //Select the employee with given ID
  30. var emp = from q in employee.Elements("Employee")
  31. where q.Element("EmpId").Value == id
  32. select q;
  33. //Return a message to the client if no data returned
  34. if (emp.Count()==0) { return "no data"; }
  35. //Iterate through emp collection to populate a list of 'employee'
  36. foreach (var element in emp)
  37. {
  38. //Gets Phone elements
  39. var e = from j in element.Elements("Phone")
  40. select j;
  41. object[] phones = e.ToArray(); //Convert phone to array in order to be able to read their values only
  42. emps.Add(new employee { Name = element.Element("Name").Value, gender = element.Element("Sex").Value, Address = element.Element("Address").Value, HomePhone = ((XElement)phones[0]).Value , WorkPhone=((XElement)phones[1]).Value });
  43. }
  44. //return emps;
  45. return new JavaScriptSerializer().Serialize(emps); //use this to return as JSON object
  46. }
  47. }
  48. public class employee
  49. {
  50. public string Name { get; set; }
  51. public string gender { get; set; }
  52. public string HomePhone { get; set; }
  53. public string WorkPhone { get; set; }
  54. public string Address { get; set; }
  55. }
  56. }

You may change how the server handles the requests to add more functionality. Now it's time to write our client-side code, and here are some different ways:

Classic JavaScript

  1. var xhttp = new XMLHttpRequest();
  2. xhttp.onreadystatechange = function () {
  3. if (xhttp.readyState == 4 && xhttp.status == 200) {
  4. var server_data = xhttp.responseXML;
  5. //When using xhttp.responseXML, data can be returned in XML format , we get 'string' TAG contents
  6. var XMLData = server_data.getElementsByTagName("string")[0].childNodes[0].nodeValue;
  7. // //Check if data found
  8. if (XMLData != "no data") {
  9. $("#txtResult").val(XMLData);
  10. //parse JSON data
  11. var Result = JSON.parse(XMLData);
  12. //Draw Header
  13. $("#table_results").append("<tr><th>Name</th><th>Gender</th><th>Address</th><th>Home Phone</th><th>Work Phone</th></tr>");
  14. //Display data
  15. $("#txtResult").val(Result[0].Name);
  16. $("#table_results").append("<tr><td>" + Result[0].Name + "</td><td>" + Result[0].gender + "</td><td>" + Result[0].Address + "</td><td>" + Result[0].HomePhone + "</td><td>" + Result[0].WorkPhone + "</td></tr>");
  17. } else { $("#txtResult").val("No Data found"); }
  18. }
  19. };
  20. xhttp.open("POST", "http://localhost:56998/EmpReader.asmx/getEmpData", true);
  21. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  22. xhttp.send("id=" + $("#txtEmpId").val() );

Using $.post method

  1. $.post("http://localhost:56998/EmpReader.asmx/getEmpData",
  2. { id: $("#txtEmpId").val() },
  3. function (data) {
  4. //Get data received from the server in XML format
  5. var data_packet = data.getElementsByTagName("string")[0].childNodes[0].nodeValue;
  6. //if invalid number sent, type an error message
  7. if (data_packet == "no data") $("#txtResult").val("No Data Found");
  8. else //if data found , draw the table
  9. {
  10. //parse JSON data
  11. var json_data = JSON.parse(data_packet);
  12. $("#txtResult").val(json_data[0].Name);
  13. //Draw the table
  14. //1. Draw the header
  15. $("#table_results").append("<tr><th>Name</th><th>Gender</th><th>Address</th><th>Home Phone</th><th>Work Phone</th></tr>");
  16. //Draw table body and write data
  17. $("#table_results").append("<tr><td>" + json_data[0].Name + "</td><td>" + json_data[0].gender + "</td><td>" + json_data[0].Address + "</td><td>" + json_data[0].HomePhone + "</td><td>" + json_data[0].WorkPhone + "</td></tr>");
  18. }
  19. });

Using $.ajax method

  1. $.ajax({
  2. type: "POST",
  3. contentType: "application/json; charset=utf-8",
  4. url: "http://localhost:56998/EmpReader.asmx/getEmpData",
  5. data: '{id:"' + $("#txtEmpId").val() + '"}',
  6. dataType: "json",
  7. success: function (data) {
  8. //Check if there is data returned from the server
  9. if (data.d != "no data") {
  10. //return all data from the server
  11. var Result = JSON.parse(data.d);
  12. //Draw Header
  13. $("#table_results").append("<tr><th>Name</th><th>Gender</th><th>Address</th><th>Home Phone</th><th>Work Phone</th></tr>");
  14. $("#txtResult").val(Result[0].Name);
  15. $("#table_results").append("<tr><td>" + Result[0].Name + "</td><td>" + Result[0].gender + "</td><td>" + Result[0].Address + "</td><td>" + Result[0].HomePhone + "</td><td>" + Result[0].WorkPhone + "</td></tr>");
  16. } else { $("#txtResult").val("No data found"); } //return error message
  17. },
  18. error: function (result) {
  19. alert("Error reading data");
  20. }
  21. });
Using AngularJS
  1. var app = angular.module("NGMainApp", []);
  2. app.controller("MainController", function ($scope, $http) {
  3. $scope.LoadEmpData = function (variable) {
  4. document.getElementById("span1").innerText = variable;
  5. //*******
  6. var sentPacket = {
  7. method: 'POST',
  8. url: 'http://localhost:56998/EmpReader.asmx/getEmpData',
  9. headers: {
  10. 'Content-Type': 'application/json'
  11. },
  12. data: { "id": $scope.NGEmpID }
  13. }
  14. //*********
  15. $http(sentPacket).then(function (response) {
  16. //alert(response.data.d);
  17. if (response.data.d != "no data") {
  18. $scope.IsVisible = true;
  19. $scope.contents = JSON.parse(response.data.d);
  20. } else { $scope.IsVisible = false; }
  21. });
  22. }
  23. });

Notice: In methods 1 and 2, we get the response from the server as XML node and parse it as JSON data.

Due to the size limit, I uploaded the project here.
Decryption code: !1GKQNhG3V4OT3EstM14ayPYd6-j4VOz7ry_c_UKUpIw