发布xml数据时,WCF Restful服务获取错误400(错误请求)
我正在尝试自托管一个WCF服务,并通过javascript调用这些服务。当我通过Json而不是xml传递请求数据(400个错误请求)时,它就会工作。请帮忙 合同:发布xml数据时,WCF Restful服务获取错误400(错误请求),xml,wcf,rest,post,xmlhttprequest,Xml,Wcf,Rest,Post,Xmlhttprequest,我正在尝试自托管一个WCF服务,并通过javascript调用这些服务。当我通过Json而不是xml传递请求数据(400个错误请求)时,它就会工作。请帮忙 合同: public interface iSelfHostServices { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = INFOMATO.RestTemplate.hello_post2,RequestFormat = WebMessage
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);
}
服务器端代码:
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) {...}
}
函数testSelfHost\u WCFService\u post\u参数(){
var xmlString=“'hello via Post'”;
Ajax_sendData(“hello/post2”,xmlString);
}
函数Ajax\u sendData(url、数据){
var请求=false;
请求=getHTTPObject();
如果(请求){
request.onreadystatechange=函数(){
解析响应(请求);
};
打开(“post”,url,true);
setRequestHeader(“内容类型”,“text/xml;charset=utf-8”);charset=utf-8”);
请求发送(数据);
返回true;
}
}
函数getHTTPObject(){
var xhr=false;
if(window.XMLHttpRequest){
xhr=newXMLHttpRequest();
}如果(window.ActiveXObject){…}
}
这是因为WCF希望您传递的字符串使用Microsoft序列化命名空间进行序列化。如果您发送
<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>hello via Post</string>
hello via Post
然后它可能会正确地反序列化。这是因为WCF希望您传递的字符串使用Microsoft序列化命名空间进行序列化。如果您发送
<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>hello via Post</string>
hello via Post
然后它可能会正确地进行反序列化。在发送XML标记时,您需要在WebInvoke属性中将body style设置为Bare,如下面的代码段所示
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = INFOMATO.RestTemplate.hello_post2,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Stream hello_post2(string helloString);
在发送XML标记时,需要在WebInvoke属性中将body style设置为Bare,如下面的代码段所示
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = INFOMATO.RestTemplate.hello_post2,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Stream hello_post2(string helloString);
Darrel,谢谢。我尝试了你的建议。但我仍然得到了相同的错误代码400。出于某些原因,如果我通过xml传递任何参数,服务无法识别该函数。如果我修改了该函数,使其没有传递任何参数,则该函数被成功调用。还有其他建议吗?@Wayne尝试将字符串放入CDA中TA section.Darrel,谢谢。我尝试了你的建议。但我仍然得到了相同的错误代码400。由于某些原因,如果我通过xml传递任何参数,服务无法识别该函数。如果我修改了该函数,因此没有传递参数,则该函数被成功调用。还有其他建议吗?@Wayne尝试将字符串放入nside一个CDATA区域。