涉及到的技術概念 WCF,REST,AJAX,JSON
遇到的問題:前台頁面使用ajax請求后台rest服務,get請求沒有問題,post請求時,后台接收參數總是null
rest方法屬性,請求參數和返回參數都是json格式
#region 創建數據庫配置 /// <summary> /// 創建數據庫配置 /// </summary> /// <param name="connectionconfig">connectionconfig表實體</param> /// <returns>返回受影響行數</returns> [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "CreateConnectionConfig", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)] public bool CreateConnectionConfig(ConnectionConfig connectionConfig) { ... } #endregion
chome控制台的調試結果
問題表現在 connectionconfig接收值總是null
如果修改信息風格為bare
WebMessageBodyStyle.Bare
connectionconfig不是null但是其屬性是null
最終的解決方法:
將json值得connectionconfig 與rest方法的參數名稱connectionConfig一致,主要是大小寫一致
后台就不會出現null值
原因不詳