ASP.NET WebService 正在用 XML 标记包装我的 JSON 响应 [英] ASP.NET WebService is Wrapping my JSON response with XML tags

查看:21
本文介绍了ASP.NET WebService 正在用 XML 标记包装我的 JSON 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我遗漏了什么.

I'm not sure where I'm going wrong of what I'm missing.

我正在构建一个 ASP.NET 2.0(在 .Net 3.5 框架上)Web 应用程序,我正在包括一个网络服务.请注意,这不是一个 MVC 项目.我希望公开一个返回 JSON 字符串的方法;格式化以提供 jqGrid jQuery 插件.

I'm building an ASP.NET 2.0 (on the .Net 3.5 framework) Web application and I am including a webservice. Note that this is not an MVC project. I wish to expose a method which will return a JSON string; formatted to feed the jqGrid jQuery plugin.

这是我在我的服务中实现的初步测试方法:感谢 (Phil Haack 的 MVC 指南)

This is the preliminary test method I've implemented in my service: thanks to (Phil Haack's Guide for MVC)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getData()
{
    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();
}

调用时返回(格式清晰):

When invoked this is returning (formatted for clarity):

<?xml version="1.0" encoding="utf-8" ?> 
<string  mlns="http://tempuri.org/">
{
  "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"]}
    ]
}
</string> 

如果没有 xml 包装,我将如何实现上述响应?

How would I achieve the above response without the xml wrappings?

推荐答案

你可能不会做的三件事:

Three things you may not be doing:

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

可能有一种方法可以使用 GET 调用该方法,我只使用过 POST.我能够让你的例子与这个一起工作:

There may be a way to call the method with a GET, I've only ever used POST. I was able to get your example working with this:

<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>

后面的代码(你不需要创建webservice,你可以把它放在你的default.aspx中):

The code behind (you don't need to create a webservice, you can put this in your 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"]}]}"}

更详细的解释是这里

这篇关于ASP.NET WebService 正在用 XML 标记包装我的 JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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