本文介绍了如何将jquery中的json数据传递给wcf服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Type = GET; // Url =http:// localhost:12344 / SecondaryToPrimaryRESTful.svc / JSON / GetClientDetailFromPrimary / + $(#txtTokenNumber)。val()+/+ $(#txtMobileNo)。val(); // Url =http:// localhost:12344 / SecondaryToPrimaryRESTful.svc / JSON / GetClientDetailFromPrimary / MobileID / 09898345474; Url = http:// localhost:12344 / SecondaryToPrimaryRESTful.svc / JSON / GetClientDetailFromPrimary; // Data ='{tokenid:'+ TokenID +',MobileNo: '+ MobileNo +'}'; // Data ='{MobileNo :''+ MobileNo +'}'; var 数据= { GetClientDetailFromPrimary:{ tokenid: + TokenID + , MobileNo: + MobileNo + }}; Data = JSON.stringify(Data); ContentType = application / json; charset = utf-8; DataType = json; ProcessData = true ; CallService(); [WebInvoke(Method =GET,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat。 Json,UriTemplate =/ GetClientDetailFromPrimary / {tokenid} / {MobileNo},BodyStyle = WebMessageBodyStyle.WrappedRequest)] public Stream GetClientDetailFromPrimary(string tokenid,string MobileNo) { WebClient proxy = new WebClient(); byte [] abc = proxy.DownloadData((new uri(http:/ /localhost:12346/PrimaryRestfulToDb.svc/JSON/GetClientData+/+ MobileNo))); Stream strm = new MemoryStream(abc); // DataContractSerializer obj = new DataContractSerializer(typeof(string)); // string result = obj.ReadObject(strm).ToString(); return strm; } 解决方案 (#txtTokenNumber)。val()+/+ (#txtMobileNo)。val(); // Url =http:// localhost:12344 / SecondaryToPrimaryRESTful.svc / JSON / GetClientDetailFromPrimary / MobileID / 09898345474; Url = http:// localhost:12344 / SecondaryToPrimaryRESTful.svc / JSON / GetClientDetailFromPrimary; // Data ='{tokenid:'+ TokenID +',MobileNo: '+ MobileNo +'}'; // Data ='{MobileNo :''+ MobileNo +'}'; var 数据= { GetClientDetailFromPrimary:{ tokenid: + TokenID + , MobileNo: + MobileNo + }}; Data = JSON.stringify(Data); ContentType = application / json; charset = utf-8; DataType = json; ProcessData = true ; CallService(); [WebInvoke(Method =GET,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat。 Json,UriTemplate =/ GetClientDetailFromPrimary / {tokenid} / {MobileNo},BodyStyle = WebMessageBodyStyle.WrappedRequest)] public Stream GetClientDetailFromPrimary(string tokenid,string MobileNo) { WebClient proxy = new WebClient(); byte [] abc = proxy.DownloadData((new uri(http:/ /localhost:12346/PrimaryRestfulToDb.svc/JSON/GetClientData+/+ MobileNo))); Stream strm = new MemoryStream(abc); // DataContractSerializer obj = new DataContractSerializer(typeof(string)); // string result = obj.ReadObject(strm).ToString(); return strm; } 看看这个:使用jQuery将JSON数据发布到WCF RESTful服务 [ ^ ] Type = "GET"; // Url = "http://localhost:12344/SecondaryToPrimaryRESTful.svc/JSON/GetClientDetailFromPrimary/" + $("#txtTokenNumber").val() + "/" + $("#txtMobileNo").val(); //Url = "http://localhost:12344/SecondaryToPrimaryRESTful.svc/JSON/GetClientDetailFromPrimary/MobileID/09898345474"; Url = "http://localhost:12344/SecondaryToPrimaryRESTful.svc/JSON/GetClientDetailFromPrimary"; // Data = '{"tokenid": "' + TokenID + '","MobileNo":"' + MobileNo + '"}'; // Data = '{"MobileNo":"' + MobileNo + '"}'; var Data = { "GetClientDetailFromPrimary": { "tokenid": "" + TokenID + "", "MobileNo": "" + MobileNo + ""} }; Data = JSON.stringify(Data); ContentType = "application/json; charset=utf-8"; DataType = "json"; ProcessData = true; CallService(); [WebInvoke(Method = "GET", RequestFormat =WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetClientDetailFromPrimary/{tokenid}/{MobileNo}", BodyStyle = WebMessageBodyStyle.WrappedRequest)] public Stream GetClientDetailFromPrimary(string tokenid, string MobileNo) { WebClient proxy = new WebClient(); byte[] abc = proxy.DownloadData((new Uri("http://localhost:12346/PrimaryRestfulToDb.svc/JSON/GetClientData" + "/" + MobileNo))); Stream strm = new MemoryStream(abc); //DataContractSerializer obj = new DataContractSerializer(typeof(string)); //string result = obj.ReadObject(strm).ToString(); return strm; } 解决方案 ("#txtTokenNumber").val() + "/" +("#txtMobileNo").val(); //Url = "http://localhost:12344/SecondaryToPrimaryRESTful.svc/JSON/GetClientDetailFromPrimary/MobileID/09898345474"; Url = "http://localhost:12344/SecondaryToPrimaryRESTful.svc/JSON/GetClientDetailFromPrimary"; // Data = '{"tokenid": "' + TokenID + '","MobileNo":"' + MobileNo + '"}'; // Data = '{"MobileNo":"' + MobileNo + '"}'; var Data = { "GetClientDetailFromPrimary": { "tokenid": "" + TokenID + "", "MobileNo": "" + MobileNo + ""} }; Data = JSON.stringify(Data); ContentType = "application/json; charset=utf-8"; DataType = "json"; ProcessData = true; CallService(); [WebInvoke(Method = "GET", RequestFormat =WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetClientDetailFromPrimary/{tokenid}/{MobileNo}", BodyStyle = WebMessageBodyStyle.WrappedRequest)] public Stream GetClientDetailFromPrimary(string tokenid, string MobileNo) { WebClient proxy = new WebClient(); byte[] abc = proxy.DownloadData((new Uri("http://localhost:12346/PrimaryRestfulToDb.svc/JSON/GetClientData" + "/" + MobileNo))); Stream strm = new MemoryStream(abc); //DataContractSerializer obj = new DataContractSerializer(typeof(string)); //string result = obj.ReadObject(strm).ToString(); return strm; }Check this out: Post JSON data to WCF RESTful Service using jQuery[^] 这篇关于如何将jquery中的json数据传递给wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 16:30