asp.net web表单json的返回结果 [英] asp.net web forms json return result

查看:127
本文介绍了asp.net web表单json的返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用asp.net和Web表单。
在项目中,我ASMX Web服务

  [的WebMethod]
    公共字符串GetSomething()
    {
      //避免circual引用(父子)
      清单< RetUsers> 。解析度= repo.GetAllUser()选择(C =>新建RetUsers {USER_ID = c.User_ID时,USER_NAME = c.User_Name,Date_Expire = c.Date_Expire})了ToList()。
      串RES1 = res.ToJson();
      //扩展方法
      返回res.ToJson();
    }

和结果是这种格式。

  [
    {USER_ID:1,User_Name的:测试1,Date_Expire:空}
    {USER_ID:2,的User_Name:测试2,Date_Expire:空}
]

我如何追加标注这个结果$阿贾克斯sucess得到这个输出:


  

1 - 测试1,2 - 测试2



解决方案

返回列表,而不是,然后用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]属性 - 它会创建JSON对象作为自动返回:

  [的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公开名单< RetUsers> GetSomething()
{
  //避免circual引用(父子)
  清单< RetUsers> 。解析度= repo.GetAllUser()选择(C =>新建RetUsers {USER_ID = c.User_ID时,USER_NAME = c.User_Name,Date_Expire = c.Date_Expire})了ToList()。  返回水库;
}

和上侧JS:

  $。阿贾克斯(
{
    键入:POST,
异步:真实,
网址:YourMethodUrl,
数据:{一些数据},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
    成功:函数(MSG)
    {
        VAR resultAsJson = msg.d //你的回报结果是JS数组
        //现在你可以循环阵列上获得的每个对象
        对(在resultAsJson变种I)
        {
            VAR用户= resultAsJson [I]
            VAR USER_NAME = user.User_Name
            //这里你的价值附加到您的标签
        }
    }
})

I use asp.net and web forms. In my project I have asmx web service

[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();
    }

And result is in this format.

[
    {"User_ID":1,"User_Name":"Test 1","Date_Expire":null},
    {"User_ID":2,"User_Name":"Test 2","Date_Expire":null}
]

How can I append to label this result in $.ajax sucess to get this output:

1 - Test 1, 2 - Test 2.

解决方案

Return the list instead, and use [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attribute - it will create JSON object as return automatically:

[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,User_Name = c.User_Name,Date_Expire = c.Date_Expire }).ToList();

  return res;
}

And on JS side:

$.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的返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆