javascript
  • web-services
  • jquery
  • asp.net-ajax
  • 2012-01-04 2 views 0 likes 
    0

    jQuery 및 SOAP-XML (dataType : "xml")과 연결을 시도한 간단한 WCF 웹 서비스가 있습니다.하지만 요청을 보내면 "BAD REQUEST 오류 400 "내 서버에서. 여기 내 SOAP-XML입니다 :JQuery 및 SOAP-XML을 사용하여 WCF 웹 서비스에 연결

    var soapMessage = 
          '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> \ 
          <soap:Header> \ 
          <Action soap:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IService/HelloWorld</Action> \ 
          </soap:Header> \ 
          <soap:Body> \ 
          <HelloWorld xmlns="http://tempuri.org/"> \ 
          </HelloWorld> \ 
          </soap:Body> \ 
          </soap:Envelope>'; 
    

    이 내 $ 아약스이다 (나는 구글 크롬이 추적)

    var productServiceUrl = 'http://localhost:3523/Service.svc/HelloWorld'; 
        $.ajax({ 
           url: productServiceUrl, 
           type: "POST", 
           dataType: "xml", 
           data: soapMessage, 
           complete: endSaveProduct, 
           contentType: "text/xml; charset=\"utf-8\"", 
           async: true, 
           error: function (xhr, textStatus, errorThrown) { 
            alert(errorThrown); 
    
           } 
    
          }); 
    

    여기 요청 및 Responce 세부 사항입니다 :

    요청 Hedear

    POST /Service.svc/HelloWorld HTTP/1.1 
    
    Host: localhost:3523 
    
    Connection: keep-alive 
    
    Content-Length: 550 
    
    Origin: http://localhost:3523 
    
    X-Requested-With: XMLHttpRequest 
    
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.15 Safari/535.2 
    
    Content-Type: text/xml; charset="UTF-8" 
    
    Accept: application/xml, text/xml, */*; q=0.01 
    
    Referer: http://localhost:3523/WcfService.htm 
    
    Accept-Encoding: gzip,deflate,sdch 
    
    Accept-Language: en-US,en;q=0.8 
    
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
    

    내 응답 헤더

    HTTP/1.1 400 Bad Request 
    
        Server: ASP.NET Development Server/10.0.0.0 
    
        Date: Wed, 04 Jan 2012 14:56:06 GMT 
    
        X-AspNet-Version: 4.0.30319 
    
        Cache-Control: private 
    
        Content-Length: 0 
    
        Connection: Close 
    

    요청 페이로드 :

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">     <soap:Header><Action soap:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/HelloWorld</Action>     </soap:Header><soap:Body><HelloWorld xmlns="http://tempuri.org/"></HelloWorld></soap:Body> </soap:Envelope> 
    

    이 내 WCF 웹 서비스 경우 :

    [OperationContract] 
        [WebInvoke(Method = "POST", 
           BodyStyle = WebMessageBodyStyle.Wrapped, 
           ResponseFormat = WebMessageFormat.Xml, 
           RequestFormat = WebMessageFormat.Xml)] 
        String HelloWorld(); 
    
    +0

    나는 이것을 (.asmx) 웹 서비스와 함께 시도했지만 올바르게 작동하지만, wcf webservice를 호출 할 때 사용하려고 할 때 잘못된 요청이 리턴된다. (400) –

    답변

    7

    내가 방법을 발견했다. 나는이 솔루션을 사용 이 내 웹 서비스 인터페이스 :

    public interface IService 
    { 
        [OperationContract] 
        //[WebGet(UriTemplate = "/data?id={value}", ResponseFormat = WebMessageFormat.Json)] 
        [WebGet(ResponseFormat = WebMessageFormat.Json)] 
        string GetData(int value); 
    
    } 
    

    이 웹 서비스에서이 기능의 내 impliment입니다 :

    public string GetData(int value) 
        { 
         return string.Format("You entered: {0}", value); 
        } 
    

    가 여기 WCF 웹 servise에 연결하는 스크립트입니다 :

    <script type="text/javascript"> 
    
    
    
        $(document).ready(function() { 
         var bhRequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
          "<s:Body>" + 
          "<GetData xmlns=\"http://tempuri.org/\">" + 
          "<value>10</value>" + 
          "</GetData>" + 
          "</s:Body>" + 
         "</s:Envelope>"; 
         $("#btnWCFBasicHttp").click(function() { 
          $.ajax({ 
           type: "POST", 
           url: "Service.svc", 
           data: bhRequest, 
           timeout: 10000, 
           contentType: "text/xml", 
           dataType: "xml", 
           beforeSend: function (xhr) { 
            xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IService/GetData"); 
           }, 
           success: function (data) { 
            $(data).find("GetDataResponse").each(function() { 
             alert($(this).find("GetDataResult").text()); 
            }); 
           }, 
           error: function (xhr, status, error) { 
            alert(error); 
    
           } 
          }); 
         }); 
        }); 
    
    
    </script> 
    

    내 HTML 페이지 근처에 WCF (url: "Service.svc")가 있음을 기억하십시오.

    +0

    고마워. 정말 고맙습니다. 나는 WCF의 newbe이므로 코드를 조금 실험 해 보았다. '[WebGet (ResponseFormat = WebMessageFormat.Json)]'을 제거한 후에도 코드는 계속 작동합니다. 왜 그렇게? – franza

    +0

    WebMessageFormat.Json은 defult 구성입니다. –

    관련 문제