I have a webMethod that takes on argument from a server session userID session
[WebMethod]
public void getNotificationList(string userID)
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(t.std_notification_get_list(int.Parse(userID))));
}
}
I want to call this web method from a jQuery script and populate the data to an HTML tabel
<table id="data-table-combine" class="table table-striped table-bordered">
<thead>
<tr>
<th class="text-nowrap">Message</th>
<th class="text-nowrap">User From</th>
<th class="text-nowrap">User To</th>
<th class="text-nowrap">Date</th>
<th class="text-nowrap">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
The script is:
<script>
$(document).ready(function () {
showTableData();
//$('#submit').click(function () {
// showTableData();
// var var_name = ('#name');
// alert(var_name.valueOf());
//});
});
var showTableData = function ()
{
var sValue = '<%=HttpContext.Current.Session["userID"]%>';
$.ajax({
dataType: "json",
method: "post",
url: 'UserService.asmx/getNotificationList',
data: JSON.stringify({ userID: sValue }),
success: function (response) {
var userTable = $('#data-table-combine tbody');
userTable.empty();
$(response).each(function (index, duser) {
alert("UserID : " + sValue + ", Message: " + duser.Message);
var var_message = (duser.Message == null) ? '' : duser.Message;
var var_user_from = (duser.User_From == null) ? '' : duser.User_From;
var var_user_to = (duser.User_To == null) ? '' : duser.User_To;
var var_action = (duser.Action == null) ? '' : duser.Action;
var var_date = (duser.Date == null) ? '' : duser.Date;
userTable.append('<tr class="gradeA"><td>' +
var_Message + '</td><td>' + var_user_from + '</td><td>' + var_user_to +
'</td><td>' + var_action + '</td><td>' + var_date + '</td></tr>');
});
},
error: function (err) {
//alert(err);
}
});
};
And when the script run, the table doesn't contain any values. So, where is the problem ?? And how i can pass the parameter to the webMethod in jQuery ??
A couple of ideas to try:
Try adding a slash in front of the url so it looks for the service starting at the root:
url: '/UserService.asmx/getNotificationList',
Also try adding a contentType:
contentType: "application/json; charset=utf-8",
Edit:
You're using a serializer, so return a string. Make sure your method is static.
[WebMethod]
public static string getNotificationList(string userID)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(t.std_notification_get_list(int.Parse(userID))));
}
The important thing to know about returning json from a web method is that the response is a wrapper. Your actual data is in an object d
.
$(response.d).each(
But, you have returned a JSON string, so to access the data as an object, you have to reconvert it to an object:
var myData = JSON.parse(response.d);
Then you can do
$(myData).each(
Depending on how your data is structured you might have to make some adjustments, like myData[0].duser
or myData[0].Message
.