Yo can use the static class HttpContext.Current , however you can skip this if you declare the parameters you want to use on your method and just pass the parameters using your AJAX call
You must pass the parameters directly to the method.
I have some working examples in my Github repository , feel free to browse the code.
The PageMethod method is called as a summary:
Note: how the AJAX is used jobID jobID jobID parameter is passed along with the request and how it is used inside the transparent PageMethod
AJAX call
$.ajax({ type: 'POST', url: '<%: this.ResolveClientUrl("~/Topics/JQuery/Ajax/PageMethods_JQueryAJAX.aspx/GetEmployees") %>', contentType: 'application/json; charset=utf-8', dataType: 'json', data: '{"jobID" : ' + jobID +'}', async: false, cache: false, success: function (data) { $('#employees').find('option').remove(); $.each(data.d, function (i, item) { $('<option />').val(item.EmployeeID).text(item.FirstName).appendTo('#employees'); }); }, error: function (xhr) { alert(xhr.responseText); } });
Page Method
[WebMethod] public static List<EmployeeModel> GetEmployees(int jobID) { var ctx = new PubsDataContext(); return (from e in ctx.employee where e.job_id == jobID orderby e.fname select new EmployeeModel { EmployeeID = e.emp_id, FirstName = e.fname }).ToList(); }
source share