I have a WCF AJAX enabled web service, and I am trying to pass a value of a field in an ASP.NET page into it with JSON but I always get nothing f. I want to do something like what we did in ASP.NET web services:
request.form("SbID").toString();
My Web Service is getting in a parameter that is NULL and I think its because of what I pass in the form. The Web Service is:
[OperationContract]
[WebGet]
public ResultData GetContactsDataAndCountbyGUID(string requestGUID)
{
ResultData result = new ResultData();
common c = new common();
string request = String.Empty;
try
{
request = OperationContext.Current.RequestContext.RequestMessage.ToString();
}
catch (Exception ex)
{
}
try
{
List<CONTACT> listContacts = new List<CONTACT>();
DataSet ds = new DataSet();
ds = c.GetDataSet(requestGUID);
DataTable dt = new DataTable();
dt = ds.Tables[0];
// listContacts = CollectionHelper.ConvertTo<CONTACT>(dt);
listContacts = c.ConvertTo<CONTACT>(dt);
result.Data = listContacts;
result.Count = listContacts.Count;
}
This is the JavaScript/jQuery:
<script type="text/javascript">
//function DoUpdate(sbiId) {
// var input = '{"SbiId":"' + sbiId + '"}';
// var dataSource;
$(document).ready(function() {
**var input ='{"SbiId":"<%=guid %>"}';**
$.ajax({
url: "http://www.blah.com/services/testsService.svc/GetContactsDataAndCountbyGUID",
type: "GET",
contentType: "application/json; charset=utf-8",
data: input,
dataType: "json",
success: function(data) {
var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
console.log(data);
mtv.set_dataSource(data.d.Data);
mtv.dataBind();
}
});
// }
});
</script>
I am trying to pass the value of GUID, which is in the form by:
public string guid = null;
common c = new common();
protected void Page_Load(object sender, EventArgs e)
{
try
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<AlphaHub>();
// common c = new common();
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
string connectionId = c.GetCurrentConnection(user.RegistrationCode);
string request = "Contact" + " - " + connectionId + " - " + user.RegistrationCode;
string guid = c.recordDataRequest(user.RegistrationCode, request, connectionId, "out", "");
request = request + " - " + guid;
hubContext.Clients.Client(connectionId).CallForReport(request);
// hub
Thread.Sleep(1000);
}
How do I get this value passed in and retrieved?
ASP.Net is stateless, you're not going to be able to access a variable from the code behind. Store it in a hidden variable which you can access from jQuery.
<input type="hidden" id="hdnGuid" runat="server">
Set it in your code behind:
string guid = c.recordDataRequest(user.RegistrationCode, request, connectionId, "out", "");
hdnGuid.Value = guid;
In your jQuery accesses it:
var hdnGuid = $("#<%= hdnGuid.ClientID %>").val();
var input ='{"SbiId":"' + hdnGuid + '"}';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.