this code works for me , the only problem is it does not bind the return value id and name to labels in alert box return values shows but it does not bind the return value in label
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=txtSearch]').on('blur', function () {
$.ajax({
type: "POST",
url: "Default2.aspx/GetPersonDetails",
data: '{name: "' + $("#<%=txtSearch.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
var user = JSON.parse(response.d);
$("[id*=lblId]").val = "dd";
$('#lblName').val = user.name;
}
});
});
});
</script>
[WebMethod]
public static string GetPersonDetails(string name)
{
string jsonData = "";
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT id, name FROM person WHERE id = @Name", con))
{
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
var input = new
{
Id = sdr["id"],
Name = sdr["name"]
};
jsonData = (new JavaScriptSerializer()).Serialize(input);
}
con.Close();
}
}
return jsonData;
}