1

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

5
  • 1
    Did you check on developer tools if you actually invoked specified webmethod? Did you see http response code 200? Commented Apr 25, 2014 at 17:01
  • I checked it now, I got response 500. Any idea why? Commented Apr 25, 2014 at 17:07
  • 1
    There is an uncaught run-time exception in your code. Add try/catch block and print out the exception. Commented Apr 25, 2014 at 17:08
  • I agree Hamdi solution.using try catch block to intercept your exception.When you will have 200 you will see your json in developer tools as response of your request.Until that you cannot see it. Commented Apr 25, 2014 at 20:33
  • @Hamid, this turned to be the case. jobDetails is sent as a string, but I didn't put any quotes around it. Please answer the question so I can accept your response. Commented May 6, 2014 at 8:36

2 Answers 2

3
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().

Sign up to request clarification or add additional context in comments.

3 Comments

`JsonConvert˙ - I need to use .NET build in libraries, nothing else. Any suggestions?
Like I said, you can use any library you like. You can use the built in JavaScriptSerializer. But I highly recommend Json.NET for reasons listed on the page I linked to.
Didn't help, I didn't get anything on client. But the string json is properly filled with the string document. I tried returning that string from GetOrderInformation, but nothing received on the client. I used JavaScriptSerializer.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.