我已经创建了一个WCF宁静服务。如何在wcf服务中获取Xml作为字符串并使用javascript发送?
public string Demo(String xmlString)
{
//do stuff
}
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, Method = "Post", UriTemplate = "Demo",
BodyStyle = WebMessageBodyStyle.Wrapped)]
string Demo(String xmlString);
我通过
$(document).ready(function() {
$("#btn").click(function() {
var bhRequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<s:Body>" +
"<GetSMC xmlns=\"http://tempuri.org/\">" +
"<value><Root>MyValue</Root></value>" +
"</GetSMC>" +
"</s:Body>" +
"</s:Envelope>";
var bhReq="<![CDATA[" + bhRequest + "]]>";
alert(bhReq);
$.ajax({
url: 'http://localhost:15536/Plugins/MyPlugin/RemoteService/WebService.svc/Demo',
type: 'POST',
data: '{"xmlString":"'+ bhReq +'"}',
contentType: "text/xml",
dataType: "xml",
success: function (data) {
alert(Successfull);
},
error: function (data) {
alert('Error Occurred');
}
});
});
});
呼叫发信不会服务,并给出了
NetworkError: 405 Method Not Allowed
XML Parsing Error: not well-formed Location: moz-nullprincipal:{70ef8883-a52b-4e70-a1ca-bdf5c611c23c} Line Number 1, Column 1:
{"xmlString":"MyValue</Root></value></GetSMC></s:Body></s:Envelope>]]>"}
的错误,我也通过一些文字,是通过我的服务,但XML字符串没有通过。
我也已经完成了使用将我的服务请求和响应格式设置为json,并使用数据类型JSON从我的脚本传递数据,它也不起作用。
请给任何解决方案如何我可以将xml作为字符串值从javascript传递给我的wcf rest服务。
@Mozts给了你正确的答案。将代码:data:'{“xmlString”:“'+ bhReq +'”}'更改为data:xmlString。并添加一个[xml头文件](http://stackoverflow.com/questions/7007427/does-a-valid-xml-file-require-an-xml-declaration),如<?xml version =“1.0”?>和删除CDATA。标签。 – Roberto