栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ASP.NET WebService正在用XML标签包装我的JSON响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET WebService正在用XML标签包装我的JSON响应

您可能没有做的三件事:

  • 将方法标记为静态
  • 执行POST
  • 为jQuery中的数据传递一个空的“ {}”。

可能有一种用GET调用方法的方法,我只用过POST。我能够使您的示例与此一起工作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script><script>    // In your javascript block    $(document).ready(function()    {        $.ajax({ url: "/Default.aspx/Tester", type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", data: "{}", success: done        });    });    function done(data)    {        // Include http://www.json.org/json2.js if your browser doesn't support JSON natively        var data = JSON.parse(data.d);        alert(data.total);    }</script>

背后的代码(您无需创建Web服务,可以将其放在default.aspx中):

[WebMethod]public static string Tester(){    JavascriptSerializer ser = new JavascriptSerializer();    var jsonData = new    {        total = 1, // we'll implement later         page = 1,        records = 3, // implement later         rows = new[]{   new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},   new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},   new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}} }        };    return ser.Serialize(jsonData); //products.ToString();}

结果:

{"d":"{"total":1,"page":1,"records":3,"rows":[{"id":1,"cell":["1","-7","Is this a good question?","yay"]},{"id":2,"cell":["2","15","Is this a blatant ripoff?","yay"]},{"id":3,"cell":["3","23","Why is the sky blue?","yay"]}]}"}

更详细的解释在这里



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485437.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号