[英]C# webservice digesting json data to class
我在弄清楚如何設置我的Web服務來消化json數據變量時遇到了麻煩。 我想在POST請求中發送數據並將其分配給類。
我的函數(見下文)該類始終為null OrderReprint所以我錯過了一大步。 任何幫助都會很棒。
public Boolean Test(OrderReprint oreprints)
{
Debug.WriteLine(OrderReprints.seq1);
Debug.WriteLine(OrderReprints.client);
Debug.WriteLine(OrderReprints.Filename);
Debug.WriteLine(OrderReprints.formcode);
Debug.WriteLine(OrderReprints.jobnum);
Debug.WriteLine(OrderReprints.userid);
Debug.WriteLine(OrderReprints.seq1);
return true;
}
我的界面看起來像這樣
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Test")]
Boolean Test(OrderReprint oreprints);
我的數據合同類看起來像這樣
[DataContract]
public class OrderReprint
{
[DataMember]
public string Filename;
[DataMember]
public string seq1;
[DataMember]
public string jobnum;
[DataMember]
public string formcode;
[DataMember]
public string userid;
[DataMember]
public string client;
}
我的調用Ajax調用如下所示:
$.ajax({
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
xhrObj.setRequestHeader("Accept", "application/json");
},
type: "POST",
url: URL,
dataType: "text",
data: JSON.stringify(params),
}).then(function (data) {
if (data.results) {
/* (done something here*/
}
}).fail(function (e) {
errorAlert('Process Job', 'Error retriving job informaiton!');
});
我從OperationContract中刪除了BodyStyle並且它有效!
BodyStyle = WebMessageBodyStyle.WrappedRequest
,
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Test")]
bool Test(OrderReprint oreprints);
謝謝大家的幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.