Reputation: 595
i am new in jquery ajax.i want to call web service but in not working. this is my jquery code.
$(document).ready(function () {
$('#TxBx_BasicSalary').focusout(function () {
var EmployeeId = $('#Hid_EmpID').val();
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: '/WebService/IncDedWebService.asmx/GetInceDed',
data: JSON.stringify({ id: EmployeeId }),
dataType: 'json',
success: function (data) {
alert("")
},
error: function () { alert("error"); }
});
});
this is WebService Method.
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
//var abc salary=.GetIncDedByEmpId(id);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(salary.GetIncDedByEmpId(id));
return json;
}
this is not working when ever i call.it execute error portion. please help me.what i did wrong.
Upvotes: 2
Views: 1981
Reputation: 107237
You haven't posted the exact error message, but there are a couple of things to look for:
Note that you've specified POST
in your $.ajax
call, whereas your ScriptMethod
has UseHttpGet = true
. I've assumed POST
.
The class containing the Web / Script methods must have the [System.Web.Script.Services.ScriptService]
in order to be callable from ajax (as per the comment added by the asmx
code Template)
The following server code works for me:
[WebService(Namespace = "http://YourNameSpaceGoesHere/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class IncDedWebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
//var abc salary=.GetIncDedByEmpId(id);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(new ClsSalary
{
Amount = 1234,
Id = 123,
Name = "Basic Salary"
});
return json;
}
}
public class ClsSalary
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
}
The json returned is:
{"d":"{\"Id\":123,\"Name\":\"Basic Salary\",\"Amount\":1234}"}
Upvotes: 4
Reputation: 198
try these changes:
$(document).ready(function () {
$('#TxBx_BasicSalary').focusout(function () {
var EmployeeId = $('#Hid_EmpID').val();
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: '/WebService/IncDedWebService.asmx/GetInceDed',
data: '{ "id": "' + EmployeeId + '" }', //THIS LINE
dataType: 'json',
success: function (data) {
var emp = $.toJson(data.d); //THIS
alert(emp.IncDeb.EmpID); //AND THIS
},
error: function () { alert("error"); }
});
});
this is WebService Method.
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
var abc salary=.GetIncDedByEmpId(id);
string json = "{\"IncDeb\":[\"EmpId\":\"" + abc.EmpId +"\"]"; //And you have to keep going with the other members
return json;
}
Upvotes: 1