Javascript 准备要在WCF服务中传递的Json字符串时出现问题。
我试图在javascript应用程序和WCF服务之间进行通信。我创建的WCF服务提供以下方法:Javascript 准备要在WCF服务中传递的Json字符串时出现问题。,javascript,json,web-services,wcf,Javascript,Json,Web Services,Wcf,我试图在javascript应用程序和WCF服务之间进行通信。我创建的WCF服务提供以下方法: [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/ExportToXml")] void ExportToXml(List<Span&g
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/ExportToXml")]
void ExportToXml(List<Span> spans, List<Detection> detections);
[DataContract]
public class Detection
{
[DataMember]
public int TID { get; set; }
[DataMember]
public double Longitude { get; set; }
[DataMember]
public double Latitude { get; set; }
[DataMember]
public double Height { get; set; }
[DataMember]
public int SN { get; set; }
[DataMember]
public string TLine_Name { get; set; }
}
[DataContract]
public class Span
{
[DataMember]
public int SN { get; set; }
[DataMember]
public double Longitude { get; set; }
[DataMember]
public double Latitude { get; set; }
[DataMember]
public string TLine_Name { get; set; }
}
服务不喜欢上面的json输入。在这件事上的任何帮助都是非常感谢的 我可以将JSON对象发送到我自己机器上运行的服务。因此,问题不在于发送到服务的JSON对象的格式。你还有一个问题。关于客户机的更多细节可能有助于解决问题 我知道您的客户端是一个JavaScript应用程序,但下面是我的C#客户端,它成功地将JSON对象发送到服务:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9001/WCFServices/RestfulService/ExportToXml");
request.ContentType = "text/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = @"{ ""spans"": " +
@"[{ ""SN"": 1, ""Longitude"": 1000000,""Latitude"": 1000000,""TLine_Name"": ""Circuit Test 1""}, " +
@"{""SN"": 2, ""Longitude"": 2000000, ""Latitude"": 2000000, ""TLine_Name"": ""Circuit Test 2"" }]," +
@"""detections"": " +
@"[{ ""TID"": 1, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 15,""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 2, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 12, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 3, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 14, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 4, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 10, ""SN"": 2, ""TLine_Name"": ""Circuit Test 2"" }, " +
@"{ ""TID"": 5, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 8, ""SN"": 1, ""TLine_Name"": ""Circuit Test 2"" }] }";
streamWriter.Write(json);
}
WebResponse ws = request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();
responseStream.Close();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9001/WCFServices/RestfulService/ExportToXml");
request.ContentType = "text/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = @"{ ""spans"": " +
@"[{ ""SN"": 1, ""Longitude"": 1000000,""Latitude"": 1000000,""TLine_Name"": ""Circuit Test 1""}, " +
@"{""SN"": 2, ""Longitude"": 2000000, ""Latitude"": 2000000, ""TLine_Name"": ""Circuit Test 2"" }]," +
@"""detections"": " +
@"[{ ""TID"": 1, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 15,""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 2, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 12, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 3, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 14, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
@"{ ""TID"": 4, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 10, ""SN"": 2, ""TLine_Name"": ""Circuit Test 2"" }, " +
@"{ ""TID"": 5, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 8, ""SN"": 1, ""TLine_Name"": ""Circuit Test 2"" }] }";
streamWriter.Write(json);
}
WebResponse ws = request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();
responseStream.Close();