0

This is my JSON return response now with using Fiddler or POSTMAN to test JSON.

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

This is what I want with "d" to "body":

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

This is my asmx code to get the Response with this 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;
}

My Response Class:

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

    public string errorcode { get; set; }

    public string errormessage { get; set; }

    public string resend { get; set; }
}

Attention: My asmx didnt use jquery ajax or aspx page in this solution to return the JSON response. Because I'm using .NET 4.5 not 2.0 and I'm also understand this "d" is a secure object. So any other ways can straight the the object "d" to "body" only?

UPDATED: Now I add my solution with an aspx page. This is my code. Is it correct? And how the ajax connect to my asmx Test() function?

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

UPDATED: Fixed it with WCF C# Service. More convenience to use this than asmx.

kennethwkz
  • 25
  • 2
  • 7

1 Answers1

0
$.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);
 }
});
Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104