How to Post Json and Retrieves Values

Posted by Jayakumars under ASP.NET AJAX on 8/13/2013 | Points: 10 | Views : 1045 | Status : [Member] [MVP] | Replies : 0
Hi

I post piece of code for this


$(document).ready(function () {
GetCustomer('Labour');
GetCustomer('Staff');
});



function GetCustomer(Rec1) {
var MthName = Rec1;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "../Service.asmx/" + MthName
data: "{}",
dataType: "json",
success: function (data) {
var items = '<option>Select</option>';
var itemss = '<option>Select</option>';
var itemsss = '<option>Select</option>';

if (MthName == 'labour') {
$.each(data.d, function (key, value) {
items += "<option value='" + value.EId + "'>" + value.Ename + "</option>";
$('#ddl1').html(items);
});

$.each(data.d, function (key, value) {
itemss += "<option value='" + value.EId + "'>" + value.Ename + "</option>";
$('#ddl2').html(itemss);
});
}
else {
$.each(data.d, function (key, value) {
itemsss += "<option value='" + value.Eid + "'>" + value.Empno + "</option>";
$('#ddl3').html(itemsss);
$('#ddl3').bind("change", function () {
GetEmp($(this).val())
});


});
}
},
error: function (result) {
alert("Error");
}
});
}



function GetEmp(ID) {
if (ID > 0) {
$.ajax({
type: "POST",
url: "../Service.asmx/ByID",
data: "{ID:" + ID + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$.each(data.d, function (key, value) {
$("#Txt1").val(value.Desc1);
$('#Img1').attr('src', '../Image/' + value.image1);
});
},
error: function () {
alert("Failed to load names");
}
});
}

}


Methods

[WebMethod]
public EmpDet[] GetEmpList()
{
DataTable dt = new DataTable();
List<EmpDet> objEmp = new List<EmpDet>();
cmd = new SqlCommand("Proc_EmpDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr1 = cmd.ExecuteReader();

while (dr1.Read())
{
EmpDet eu = new EmpDet();
eu.Id = (int)dr1["Id"];
eu.name = dr1["name"].ToString();
objEmp.Add(eu);
}

return objemp.ToArray();
}

Mark as Answer if its helpful to you


Responses

(No response found.)

Login to post response