我有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 { ... }
我无法在客户端获得任何东西。在此之前,我尝试从另一个标记为GetOrderLookupResponseType
的方法返回[WebMethod]
并且它有效,但是当我尝试将其放入新创建的OrderInformationResponse
时,它停止了工作。 (我打算用OrderInformationResponse
填写其他内容,这就是为什么我没有使用有效的方法。
答案 0 :(得分: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方法不能很好地处理JSON响应。因此,将JSON直接写入输出并将签名设置为void
。我使用Json.NET来执行JSON,但任何其他人通常都应该工作。
此外,您可以更改签名,使其接受字符串作为唯一参数,然后将该JSON字符串转换为OrderInformationRequest
中的GetOrderInformation()
。
答案 1 :(得分:0)
我收到HTTP错误500。 jobDetails
以字符串形式发送,但我没有在其周围添加任何引号。