Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir/Mam,

I am trying to call WebMethod on asp.net web page using ajax. occurring 500 Internal server error and method is not calling.
My Code is :
-------------
On ASPX Page
-------------

function GetData(ID) {
$.ajax({
url: '../ITS/Reports.aspx/GetReport',
method: 'post',
contentType: 'application/json; charset=utf-8',
data: {ID: ID},
dataType: 'json',
async: true,
cache: false,
success: function (data) {
}
});
}
-----------------------
On C# Reports.aspx.cs
----------------------

[System.Web.Services.WebMethod]
public bool GetReport(int ID)
{
Globel gbl = new Globel();

DataTable Dt = gbl.GetData(ID);

return true;
}

Please help me what is the mistake in my code.
Thank You...

What I have tried:

Trying to call WebMethod on asp.net web page using ajax.
Posted
Updated 3-Nov-17 4:02am
Comments
Karthik_Mahalingam 3-Nov-17 7:45am    
check these

* make sure the url is accessible
* stringify the data parameter
* change the method to static

refer this code, check the inline comments

[System.Web.Services.WebMethod]
      public static bool GetReport(int ID)   // make it as static
      {

          // Your code

          return true;
      }


function GetData(id) {
           var ID = parseInt(id);  // make sure you are passing the integer value
           var obj = { ID: ID };
           var param = JSON.stringify(obj);  // stringify the parameter

           $.ajax({
               url: '/ITS/Reports.aspx/GetReport', // remove the ..
               method: 'post',
               contentType: 'application/json; charset=utf-8',
               data:  param,
               dataType: 'json',
               async: true,
               cache: false,
               success: function (data) {
                   alert(data.d); // true
               },
               error: function (xhr, status, error) {
                   alert(xhr.responseText);  // to see the error message
               }
           });
       }

       GetData(1);
 
Share this answer
 
Comments
Rahul Newara 4-Nov-17 2:56am    
Thank You Sir
Its working...
Karthik_Mahalingam 4-Nov-17 3:33am    
welcome
500 errors mean the code on the server crashed or failed for some reason.

That could be because you sent parameters it didn't expect, sent them in a format it didn't expect, values out of range, or the code on the server is poorly written and doesn't check the input parameters before trying to process them, or some combination of the above.
 
Share this answer
 

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



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