WCF Restful services getting error 400 (bad request) when post xml data
Posted
by Wayne Lo
on Stack Overflow
See other posts from Stack Overflow
or by Wayne Lo
Published on 2010-06-16T00:02:07Z
Indexed on
2010/06/16
0:22 UTC
Read the original article
Hit count: 1269
I am trying to self host a WCF services and calling the services via javascript. It works when I pass the request data via Json but not xml (400 bad request). Please help.
Contract:
public interface iSelfHostServices
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = INFOMATO.RestTemplate.hello_post2,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream hello_post2(string helloString);
}
Server side code:
public Stream hello_post2(string helloString)
{
if (helloString == null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
return null;
}
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
return new MemoryStream(Encoding.UTF8.GetBytes(helloString));
}
JavaScript:
function testSelfHost_WCFService_post_Parameter() {
var xmlString = "<helloString>'hello via Post'</helloString>";
Ajax_sendData("hello/post2", xmlString);
}
function Ajax_sendData(url, data) {
var request = false;
request = getHTTPObject();
if (request) {
request.onreadystatechange = function() {
parseResponse(request);
};
request.open("post", url, true);
request.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); charset=utf-8");
request.send(data);
return true;
}
}
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {...}
}
© Stack Overflow or respective owner