asp.net web表单json返回结果
发布时间:2020-12-16 00:45:39 所属栏目:asp.Net 来源:网络整理
导读:我使用asp.net和web表单。 在我的项目中,我有asmx web服务 [WebMethod] public string GetSomething() { // avoid circual reference(parent child) ListRetUsers res = repo.GetAllUser().Select(c = new RetUsers {User_ID = c.User_ID,User_Name = c.Use
我使用asp.net和web表单。
在我的项目中,我有asmx web服务 [WebMethod] public string GetSomething() { // avoid circual reference(parent child) List<RetUsers> res = repo.GetAllUser().Select(c => new RetUsers {User_ID = c.User_ID,User_Name = c.User_Name,Date_Expire = c.Date_Expire }).ToList(); string res1 = res.ToJson(); // extension methods return res.ToJson(); } 结果是这种格式。 [ {"User_ID":1,"User_Name":"Test 1","Date_Expire":null},{"User_ID":2,"User_Name":"Test 2","Date_Expire":null} ] 如何附加标签,以$ .ajax的成功获取此输出:
解决方法
返回列表,并使用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]属性 – 它将创建JSON对象作为自动返回:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<RetUsers> GetSomething() { // avoid circual reference(parent child) List<RetUsers> res = repo.GetAllUser().Select(c => new RetUsers {User_ID = c.User_ID,Date_Expire = c.Date_Expire }).ToList(); return res; } 在JS方面: $.ajax( { type: "POST",async: true,url: YourMethodUrl,data: {some data},contentType: "application/json; charset=utf-8",dataType: "json",success: function(msg) { var resultAsJson = msg.d // your return result is JS array // Now you can loop over the array to get each object for(var i in resultAsJson) { var user = resultAsJson[i] var user_name = user.User_Name // Here you append that value to your label } } }) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Asp.Net MVC5代码的筛选器(ActionFilter)执行递归链表分析
- iis – 经典ASP上的500服务器错误 – 无法获得更多详细信息
- asp.net-mvc – 将HttpRequestMessage转换为HttpRequest
- 验证 – ASP.Net MVC2:ModelState是无效的,但我不知道为什
- asp.net-mvc – 带有淘汰赛的EditorFor HTML Helper
- asp.net-mvc – 在ASP.NET MVC应用程序中托管WCF服务?
- ASP.NET MVC 3应用程序中每个浏览器选项卡/窗口的新会话
- asp.net-mvc – MVC – 如何更改帖子中文本框的值?
- 什么是ASP.NET中的gpstate文件
- .net – 具有预编译视图的助手
推荐文章
站长推荐
- asp.net – 在Web api控制器中手动验证模型
- asp.net – MVC Child Action在Path中抛出非法字
- asp.net-mvc – ASP.net MVC 4从数据库加载菜单到
- asp.net – 表单认证ReturnUrl和子域名用于单点登
- .Net Core微服务入门全纪录(三)——Consul-服务
- asp.net-mvc-2 – 如何设置RadioButtonFor()在AS
- asp.net-mvc – 无法更改关系,因为一个或多个外键
- ASP.NET Web API:OAuth服务提供商
- asp.net-mvc – 我的viewmodel值类型属性应该可以
- 在ASP.NET中动态设置元素属性的值
热点阅读