【发布时间】: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 更方便。
【问题讨论】: