Reputation: 59
I have created one WCF restful service.
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);
I am sending through
$(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');
}
});
});
});
Call is not going to service and gives error of
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>]]>"}
I have also passed some text that is pass in my service but xml string is not passing.
I have also done using set my service request and response format to json and pass data from my script using datatype json it's also not working.
Please give any solution how i can pass xml as a string value from javascript to my wcf rest service.
Upvotes: 1
Views: 1067
Reputation: 319
The way user it you have two options one to change the wcf to format the messages as json or to setup the xml for soap correctly
For option one read http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
For option two You can read https://leonidius2010.wordpress.com/2011/05/16/98/
For this option I see a few issues with your example xml first off you are wrapping the full xml in cdata which is not correct format.
I also see that the xml doc type is missing in the payload you are sending.
To get the correct format for JavaScript I would use the wcf test client in visual studio to call your end point and then copy and past payload being sent in to my JavaScript file.
Upvotes: 1