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

    私は単純なWCF Webサービスを持っていますjqueryとSOAP-XML(dataType: "xml")と接続しようとしましたが、リクエストを送信すると "BAD REQUESTエラー400 "私のサーバーから。 は、ここに私のSOAP-XMLです:JQueryとSoap-XMLを使用してWCF Webサービスに接続

    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>'; 
    

    、これが私の$アヤックスである:(私はGoogle Chromeでこれをトレース)

    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 Webサービス場合:

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

    これを(.asmx)Webサービスで試してみましたが、正常に動作しますが、wcf webserviceを呼び出すときに使用したい場合は、Bad Request(400) –

    答えて

    7

    私は方法を見つけました。私は、このソリューションを使用する: これは私のWebサービスのインタフェースです:

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

    これは、Webサービスでは、この機能の私のimplimentです:

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

    、ここでは、スクリプトは、WCFのWeb 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> 
    

    WCF(url: "Service.svc"が)私のhtmlページの近くであることを覚えています。

    +0

    ありがとうございます。どうもありがとうございます。私はあなたのコードを少し実験したので、WCFでnewbeです。 '[WebGet(ResponseFormat = WebMessageFormat.Json)]を削除すると、コードは引き続き動作します。なぜそうなのか? – franza

    +0

    WebMessageFormat.Jsonはデフルト設定です –

    関連する問題