【问题标题】:ASMX JSON NET 4.5 return response wrapped with "d", How to change the "d" to "body"?ASMX JSON NET 4.5 返回用“d”包装的响应,如何将“d”更改为“body”?
【发布时间】:2016-03-30 07:20:38
【问题描述】:

这是我现在使用 Fiddler 或 POSTMAN 测试 JSON 的 JSON 返回响应。

{
  "d": {
    "result": null,
    "errorcode": "test",
    "errormessage": "Invalid signature.",
    "resend": null
  }
}

这就是我想要的“d”到“body”:

{
  "body": {
    "result": null,
    "errorcode": "test",
    "errormessage": "Invalid signature.",
    "resend": null
  }
}

这是我的 asmx 代码,用于通过此 url -http://localhost:59583/JSONTest.asmx/Test 获取响应

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Response Test(RequestHeader header)
{
    Response response = new Response();
    response.result = "N";
    response.errorcode = "test";
    response.errormessage = "test";
    response.resend = "N";

    return response;
}

我的响应类:

public class Response
{
    public string result { get; set; }

    public string errorcode { get; set; }

    public string errormessage { get; set; }

    public string resend { get; set; }
}

注意:我的 asmx 在此解决方案中没有使用 jquery ajax 或 aspx 页面来返回 JSON 响应。因为我使用的是 .NET 4.5 而不是 2.0,而且我也明白这个“d”是一个安全对象。那么还有其他方法可以将对象“d”直接指向“body”吗?

更新:现在我用一个 aspx 页面添加我的解决方案。这是我的代码。这是正确的吗?以及 ajax 如何连接到我的 asmx Test() 函数?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
        function example() {
            //var result = "";
            $.ajax({
                type: "POST",
                url: "JSONTest.asmx/Test",
                data: "{ }",
                contentType: false,
                success: function (result) {
                    var body = JSON.parse(result.d);
                    // you can access your object here
                    console.log(body.result);
                },
                dataType: "json"//set to JSON    
            })
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>

    </form>
</body>
</html>

更新:使用 WCF C# 服务修复它。使用它比 asmx 更方便。

【问题讨论】:

标签: c# json .net-4.5 asmx


【解决方案1】:
$.ajax({
   url: 'your-url-here',
   type: "post",
   contentType: false,
   success: function (result) {
   var body = JSON.parse(result.d);
      // you can access your object here
      console.log(body.result);
 }
});

【讨论】:

  • 这意味着我必须通过脚本在 aspx 中使用 jquery ajax?
  • 我不太明白如何使用result.body。有没有其他的方式来形容这个?
  • 您将无法将“d”更改为其他值。检查我编辑的答案。让我知道。
  • 因为你的对象是用“d”包裹的,所以你必须先用 JSON.parse(d);
  • 查看如何在 asmx .net 4.5 中执行 ajax 的任何链接?
猜你喜欢
  • 2013-08-19
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2010-12-26
  • 2011-05-01
相关资源
最近更新 更多