Robotronx
Robotronx

Reputation: 1818

ASP.NET return JSON object via WebMethod

I have ASP.NET code

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

I try calling this from jQuery with:

$.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.
            }
        });

Declaration of 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 { ... }

I can't get anything on client side. Before this I was trying to return GetOrderLookupResponseType from another method marked [WebMethod] and it worked, but when I tried to put it in a newly created OrderInformationResponse it stopped working. (I plan to fill OrderInformationResponse with additional stuff, that's why I'm not using the method that worked).

Upvotes: 1

Views: 6437

Answers (2)

mason
mason

Reputation: 32694

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 Methods doesn't handle JSON responses very well. So write the JSON directly to the output and set the signature to void. I used Json.NET to do the JSON, but any other one should generally work.

Also, you might change up your signature so it accepts a string as the only parameter, then convert that JSON string to an OrderInformationRequest in GetOrderInformation().

Upvotes: 3

Robotronx
Robotronx

Reputation: 1818

I was getting HTTP Error 500 . jobDetails is sent as a string, but I didn't put any quotes around it.

Upvotes: 0

Related Questions