【问题标题】:ASP.NET return JSON object via WebMethodASP.NET 通过 WebMethod 返回 JSON 对象
【发布时间】:2014-04-25 16:40:36
【问题描述】:

我有 ASP.NET 代码

public class OrderInformationResponse
{
    public GetOrderLookupResponseType orderLookupResponse { get; set; }
}
[System.Web.Services.WebMethod]
public static OrderInformationResponse GetOrderInformation(string jobId, string userId, string jobDetails) {
    OrderInformationResponse response = new OrderInformationResponse();
    JobDetails jobDetailsEnum = (JobDetails)Enum.Parse(typeof(JobDetails), jobDetails);
    response.orderLookupResponse = GetOrderLookup(jobId, userId);
    return response;
}

我尝试从 jQuery 调用它:

$.ajax({
            type: "POST",
            url: "somePage.aspx/GetOrderInformation",
            data: "{'jobId':" + jobId + ", " + 
            " 'userId':" + userId + ", " +
                  " 'jobDetails':" + jobDetails +
                  "}",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            async: true,
            cache: false,
            success: function (msg) {
                p_Func(msg);           // This is a pointer to function.
            }
        });

GetOrderLookupResponseType的声明:

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.17929")] 
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]   [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.somewhere.uk/MessageContracts/OrderManagement/OrderInfo/2010/02")]
public partial class GetOrderLookupResponseType { ... }

我无法在客户端获得任何东西。在此之前,我试图从另一个标记为[WebMethod] 的方法返回GetOrderLookupResponseType 并且它有效,但是当我尝试将它放入新创建的OrderInformationResponse 时它停止工作。 (我打算用其他东西填充OrderInformationResponse,这就是为什么我没有使用有效的方法)。

【问题讨论】:

  • 如果你真的调用了指定的 webmethod,你检查过开发者工具吗?你看到 http 响应码 200 了吗?
  • 我现在检查了一下,我收到了响应 500。知道为什么吗?
  • 您的代码中有一个未捕获的运行时异常。添加 try/catch 块并打印出异常。
  • 我同意 Hamdi 解决方案。使用 try catch 块来拦截您的异常。当您有 200 个时,您将在开发人员工具中看到您的 json 作为您请求的响应。直到您看不到它。跨度>
  • @Hamid,原来是这样。 jobDetails 是作为字符串发送的,但我没有在它周围加上任何引号。请回答问题,以便我接受您的回复。

标签: c# jquery asp.net json


【解决方案1】:
public class OrderInformationResponse
{
    public GetOrderLookupResponseType orderLookupResponse { get; set; }
}

[System.Web.Services.WebMethod]
public static void GetOrderInformation(string jobId, string userId, string jobDetails)
{
    OrderInformationResponse response = new OrderInformationResponse();
    JobDetails jobDetailsEnum = (JobDetails)Enum.Parse(typeof(JobDetails), jobDetails);
    response.orderLookupResponse = GetOrderLookup(jobId, userId);
    string json = JsonConvert.SerializeObject(response); //I use Json.NET, but use whatever you want
    HttpContext.Current.Response.ContentType = "application/json";
    HttpContext.Current.Response.Write(json);
}

ASP.NET Web 方法不能很好地处理 JSON 响应。所以直接将 JSON 写入输出,并将签名设置为void。我使用Json.NET 来处理 JSON,但其他任何一个都应该可以正常工作。

此外,您可以更改您的签名,使其接受一个字符串作为唯一参数,然后将该 JSON 字符串转换为 GetOrderInformation() 中的 OrderInformationRequest

【讨论】:

  • `JsonConvert˙ - 我需要在库中使用 .NET 构建,仅此而已。有什么建议吗?
  • 就像我说的,你可以使用任何你喜欢的库。您可以使用内置的JavaScriptSerializer。但我强烈推荐Json.NET,原因列在我链接到的页面上。
  • 没有帮助,我在客户端没有得到任何东西。但是string json 正确填充了字符串文档。我尝试从GetOrderInformation 返回该字符串,但客户端没有收到任何内容。我使用了 JavaScriptSerializer。
【解决方案2】:

我收到 HTTP 错误 500 。 jobDetails 作为字符串发送,但我没有在它周围加上任何引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-03-02
    相关资源
    最近更新 更多