[英]Ajax sending null values to Controller even though I can see the value isn't null in Ajax
[英]Why would a WCF AJAX method's argument not be populated with values, even though the instance is not null?
我使用jQuery.ajax向服務器發送了AJAX請求,“數據”字段中填充了與我的ClientMessage類型相對應的JSON字符串,但數據並未將其傳遞給方法的主要參數。 當該方法在我調試時運行時,ClientMessage的所有成員在ProcessRequest的data參數中均為null。
ClientMessage類的成員是簡單的自動實現的獲取/設置字符串屬性,使用類本身的DataMember(Name =“ membername”)屬性以及DataContract屬性進行修飾。 在客戶端,Chrome的開發人員工具顯示AJAX請求的數據對象是有效的JSON,我可以在請求查看器中將其展開以驗證是否發送了正確的對象和成員。
我只是無法弄清楚為什么ClientMessage數據實例的所有成員都以null值到達(順便說一下,它們都是簡單的字符串)。
[OperationContract]
[WebInvoke(Method="POST",BodyStyle=WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
public ServerResponse ProcessRequest( ClientMessage data )
{
//all members of "data" parameter are null, why?
沒關系。 顯然,您必須在成員名稱與方法的參數名稱匹配的包裝對象中發送數據對象。 因此, data:JSON.stringify({username:"",password:""})
使用諸如data:JSON.stringify({username:"",password:""})
類的設置進行AJAX調用data:JSON.stringify({username:"",password:""})
我不得不使用data:JSON.stringify({data:{username:"",password:""})
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.