Hold On

Hold On

  • 1.1k
  • 996
  • 158.8k

Autocomplete in html input using webservice

Mar 24 2017 2:47 AM
Can someone help me find the error in my code? I trying to make an autocomplete textbox. My code run in the other pages but in this particular page it gaves me an error. Here's the code snipet:
  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $(".autosuggest").autocomplete({  
  4.             source: function (request, response) {  
  5.                 $.ajax({  
  6.                     type: "POST",  
  7.                     contentType: "application/json;charset=utf-8",  
  8.                     url: "AgentService.asmx/GetMatchingAgents",  
  9.                     data: "{'agent':'" + document.getElementById('AgentNumber').value + "'}",  
  10.                     dataType: "json",  
  11.                     success: function (data) {  
  12.                         response(data.d);  
  13.                     },  
  14.                     error: function (result) {  
  15.                         alert("Agent ID not found.");  
  16.                     }  
  17.                 });  
  18.             }  
  19.         });  
  20.     });  
  21. </script>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.         <div>  
  25.             <fieldset style="width: 500px;" class="ui-widget">  
  26.                 <legend><strong>CI Issuance to Agents</strong></legend>  
  27.                 <table>  
  28.                     <tr>  
  29.                         <td>CI Series #:  
  30.                         </td>  
  31.                         <td>  
  32.                             <asp:TextBox ID="txtCISeries" runat="server"></asp:TextBox>  
  33.                         </td>  
  34.                     </tr>  
  35.                       
  36.                     <tr>  
  37.                         <td>Issued To:  
  38.                         </td>  
  39.                         <td>  
  40.                             <input id="AgentNumber" runat="server" type="text" class="autosuggest" placeholder="Search agent name" />  
  41.                         </td>  
  42.   
  43.   
  44.   
  45.  public class AgentService : System.Web.Services.WebService  
  46.     {  
  47.   
  48.         [WebMethod]  
  49.         public List<string> GetMatchingAgents(string agent)  
  50.         {  
  51.             List<string> agents = new List<string>();  
  52.             using (SqlConnection con = DBConnection.GetDbCon())  
  53.             {  
  54.                 SqlCommand cmd = new SqlCommand("spGetMatchingAgents", con);  
  55.                 cmd.CommandType = CommandType.StoredProcedure;  
  56.                 cmd.Parameters.AddWithValue("@AgentID", agent);  
  57.                 con.Open();  
  58.                 SqlDataReader rdr = cmd.ExecuteReader();  
  59.                 while (rdr.Read())  
  60.                 {  
  61.                     agents.Add(rdr["AgentID"].ToString());  
  62.                 }  
  63.             }  
  64.             return agents;  
  65.         }  
  66.     }  
The webservice is working fine. I tried it and got the data. But when i call it from the webform it gives me this error:
 
 

Answers (3)