Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to call a web method on jquery button click event uisng jquery ajax calls, But getting Exception "A first chance exception of type System.Threading.ThreadAbortException' occurred in mscorlib.dll" in output file of visual studio.(I am using the Visaul Studio 2012 WebExpress)

XML
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

               <script type="text/javascript">
                       $(document).ready(function () {
                           $("#btnUploadFile").click(function (e) {
                               $.ajax({
                                   type: 'POST',
                                   data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
                                   url: "AllEmployees.aspx/UploadEmployees",
                                   contentType: "application/json; charset=utf-8",
                                   dataType: "json",
                                   success: function (msg) {
                                       alert(msg);
                                   }
                               });
                               e.preventDefault();
                           });
                       });
                   </script>




C#
[System.Web.Services.WebMethod]
             public string UploadEmployees(string name)
                    {
                        try
                        {
                            name="hi";
                           return msg;
                        }
                        catch (Exception)
                        {

                            throw;
                       }

                    }
Posted
Updated 29-Jul-14 21:13pm
v2
Comments
Gihan Liyanage 30-Jul-14 3:15am    
Do you use any response.redirect() function ?

You forget to set method static, try this.. :)

C#
[System.Web.Services.WebMethod]
           public static string UploadEmployees(string name)
                  {
                      try
                      {
                          name=&quot;hi&quot;;
                         return msg;
                      }
                      catch (Exception)
                      {

                          throw;
                     }

                  }
 
Share this answer
 
Comments
santoshkumar413 30-Jul-14 3:17am    
@ Nirav Prabtani Thanks for reply but when i adds static key word i am getting errors for the non static methods present inside the above method, Actually The method will be
[System.Web.Services.WebMethod]
public string UploadEmployees(string name)
{
try
{
string msg = string.Empty;
if (flUpload.HasFile)
{
Upload(); //Upload File Method
if (this.currFileExtension == ".xlsx" || this.currFileExtension == ".xls")
{
DataTable dt = ReadExcelToTable(currFilePath); //Read Excel File (.XLS and .XLSX Format)
msg = InsertUpdateEmployees(dt);
}
else if (this.currFileExtension == ".csv")
{
DataTable dt = ReadExcelWidthStream(currFilePath); //Read .CSV File
msg = InsertUpdateEmployees(dt);
}
}

return msg;
}
catch (Exception)
{

throw;
}
}
Nirav Prabtani 30-Jul-14 3:19am    
You can avoid the static constraint by using a simple .asmx page instead of the codebehind page.

http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-sideaspx-cs-from-client-side-use-javascript
santoshkumar413 30-Jul-14 3:26am    
Hi, Thnaks for the reply I will Be adding the web service nad calling that service
Nirav Prabtani 30-Jul-14 3:27am    
welcome.. :)
I modified your code. See if it works :

JavaScript
$(document).ready(function () {
          $("#btnUploadFile").click(function (e) {
              $.ajax({
                  type: 'POST',
                  data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
                  url: "SortGrid.aspx/UploadEmployees",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (msg) {
                      alert(msg.d); //here you need to use msg.d in order to get response.
                  }
              });
              e.preventDefault();
          });
      });


C#
[System.Web.Services.WebMethod]
   public static string UploadEmployees(string name)//you have to use Static key word
   {
       try
       {
           //name = "hi";
           return name; //return parameter value it will be your textbox value
       }
       catch (Exception)
       {

           throw;
       }

   }


Good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900