ASP.NET通过WebMethod返回JSON对象 [英] ASP.NET return JSON object via WebMethod
问题描述
我有ASP.NET代码
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;
}
我尝试用jQuery调用它:
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.
}
});
声明 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
,这就是为什么我没有使用有效的方法)。
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).
推荐答案
我收到HTTP错误500。 jobDetails
以字符串形式发送,但我没有在其周围添加任何引号。
I was getting HTTP Error 500 . jobDetails
is sent as a string, but I didn't put any quotes around it.
这篇关于ASP.NET通过WebMethod返回JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!