我有一个支持WCF AJAX的Web服务,我试图通过JSON将ASP.NET页面中的字段值传递给它,但我总是得不到任何东西。我想做的事情就像我们在ASP.NET Web服务中所做的那样:
的request.form(" SBID&#34)的toString();
我的Web服务正在获取一个NULL参数,我认为这是因为我在表单中传递的内容。 Web服务是:
[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;
}
这是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>
我正在尝试传递GUID的值,其形式为:
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);
}
如何传入并检索此值?
答案 0 :(得分:0)
ASP.Net是无状态的,您无法从后面的代码中访问变量。 将它存储在一个隐藏变量中,您可以从jQuery访问它。
<input type="hidden" id="hdnGuid" runat="server">
在你的代码中设置它:
string guid = c.recordDataRequest(user.RegistrationCode, request, connectionId, "out", "");
hdnGuid.Value = guid;
在你的jQuery中访问它:
var hdnGuid = $("#<%= hdnGuid.ClientID %>").val();
var input ='{"SbiId":"' + hdnGuid + '"}';