User-1330468790 posted
Hi, uniservice3
Based on your description, I suggest you could do like this, please refer to the following example:
Foreground code:
<body>
<form id="userlist_form" runat="server">
<div>
<button id="getUserBtn" type="button">get info</button>
<br />
<label id="info"></label>
</div>
</form>
<script src="Scripts/jquery-3.4.1.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("#getUserBtn").click(function () {
$.ajax({
url: "getUserListService.asmx/getUserList",
type: "POST",
data: null,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d != null) {
$("#info").html(data.d);
} else {
$("#info").html("There is no data");
}
}
});
});
});
</script>
</body>
Behind code(web service):
// datatable to Json
public string DataTableToJSON(DataTable table)
{
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(table);
return JSONString;
}
[WebMethod]
public String getUserList()
{
String jsonStr;
DataTable dt = new DataTable();
String conStr = "Server=.;Database=TestDB;Trusted_Connection=True";
using (SqlConnection con = new SqlConnection(conStr))
{
String sql = "select * from users";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
jsonStr = DataTableToJSON(dt);
return jsonStr;
}else
{
return null;
}
}
}
Result:
Page display:

Response:

I would like to know if this can help you meet your needs.
If it is another requirement, could you please describe it in detail?
Best regards,
Sean