자체 WCF 서비스를 호스팅하고 JavaScript를 통해 서비스를 호출하려고합니다. Json을 통해 요청 데이터를 전달할 때 xml (400 잘못된 요청)을 전달할 때 작동합니다. 도와주세요.WCF 게시 된 XML 데이터에 오류가 발생하여 400 (잘못된 요청)이 발생합니다.
계약 :
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));
}
자바 스크립트 : WCF 당신이 전달하는 문자열은 Microsoft 직렬화 네임 스페이스를 사용하여 직렬화 할 것으로 예상되기 때문이다
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) {...}
}
Darrel, 감사합니다. 나는 너의 제안을 시도했다. 그러나 나는 여전히 동일한 오류 코드 400을 가지고 있습니다. XML을 통해 매개 변수를 전달하면 서비스가 함수를 인식하지 못하는 데는 몇 가지 이유가 있습니다. 매개 변수가 전달되지 않도록 함수를 수정하면 함수가 성공적으로 호출되었습니다. 다른 제안? –
@Wayne 문자열을 CDATA 섹션에 넣으십시오. –