簡體   English   中英

WCF服務方法中發送的JSON參數為null

[英]Sent JSON parameter is null in WCF Service method

我想將確認列表更新到服務器,並且使用jQuery中的ajax。

問題是我發送了數據(在Fiddler中查找了該數據),但是服務未獲得該列表。

Web方法的參數是字符串和列表。 令牌正確,但列表為空。

Ajax呼叫:

$.ajax({
    url: "http://localhost:8000/teamplay/externalclientrestservice.svc/UpdateConfirmationListforAttender?accessToken=" + accessToken,
    type: "POST",
    contentType: "application/json",
    dataType: "json",
    processData: false,
    data: '{"accessToken": ' + JSON.stringify(accessToken) + ', "list": ' + JSON.stringify(confirmationList) + ' }'
}).done(function (receivedList)
    {
    alert("Änderungen wurden erfolgreich übernommen");
    return receivedList;
}).fail(function ()
    {
    alert("Etwas ist schief gegangen. Bitte später erneut versuchen.");
});

WebMethod:

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "UpdateConfirmationListforAttender?accesstoken={accessToken}")]
    List<AppointmentConfirmationListItem> UpdateConfirmationListforAttender(string accessToken, List<AppointmentConfirmationListItem> list);

在Fiddler中,我看到了從JSON字符串化的列表和accessToken。

我究竟做錯了什么?

提前致謝!

我是json的新手,但遇到了類似的問題。

我正在從服務器端(REST WCF)看它。

我發現的“解決方案”是在json字符串的開頭添加方法名稱。

在您的情況下,我建議的json字符串應為:

{“ UpdateConfirmationListforAttender”:{“ accessToken”:'+ JSON.stringify(accessToken)+',“ list”:'+
JSON.stringify(confirmationList)+'}}

我仍在尋找更“通用”的解決方案,以便我的WCF可以獲取參數,而不必在客戶端明確插入方法名稱。

嘗試將您的UriTemplate更改為此

UriTemplate = "UpdateConfirmationListforAttender/{accessToken}/{list}")]

然后從您的Ajax調用中刪除data:部分,並將URL更新為此

url: "http://localhost:8000/teamplay/externalclientrestservice.svc/
      UpdateConfirmationListforAttender/JSON.stringify(accessToken)/JSON.stringify(confirmationList)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM