3
Answers

Working with web Method and ajax

Hi 
 
 I have the following on my Calender.aspx code behind:
 
[WebMethod]
public static void saveTask(string DATE, int IDEVENT)
{
using (ProjectManagementEntities db = new ProjectManagementEntities())
{
var updateTask = db.tb_Task.FirstOrDefault(x => x.Id == IDEVENT);
updateTask.StartDate = Convert.ToDateTime(DATE);
db.SaveChanges();
}
}
 
and the following script:
 
function saveTask(theDATE, theIDEVENT) {
$.ajax({
type: "POST",
url: "Calender.aspx/saveTask",
data: JSON.stringify({ date : theDATE, id : theIDEVENT }),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('Task Updated Successfully.');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error");
}
});
};
 
The problem is I can't debug the webMethod saveTask, i put a breakpoint but t doesn't even hit the page. It seem the script does not hit the webMethod so can you please assist.
 
Thanks.
 
 
Answers (3)
2
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 7y
To resolve your issue:
 
1. Make method as public and static
2. Make sure param name should be same
3. Make sure that you shouldn't have any client side error like variable is undefined etc.. You can see error in console tab by pressing F12 in browser.
4. jQuery library reference should be added properly. 
  1. [System.Web.Services.WebMethod]  
  2. public static string saveTask(string date, string id)  
  3. {  
  4.     return "Hello " + name + Environment.NewLine + "The Current Time is: "  
  5.         + DateTime.Now.ToString();  
  6. }  
 
Accepted
0
Sbonga Mathobela

Sbonga Mathobela

NA 68 2.7k 7y
Thanx guys....I found the soultion. I changed in the RouteConfug :settings.AutoRedirectMode = RedirectMode.Off; instead of settings.AutoRedirectMode = RedirectMode.Permanent
0
Pooja Dixit

Pooja Dixit

NA 29 6 7y
It is May be possible in Server side Error Occured..
that time debugger is not properly working on..