asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net web表单json返回结果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用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 web表单json返回结果全部内容,希望文章能够帮你解决asp.net web表单json返回结果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。