Ajax在调用含有SoapHeader的webservice方法

·         [WebService(Namespace = "http://tempuri.org/")]
·             [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
·             public class WebService4JQuery : System.Web.Services.WebService
·             {
·                 
·                 public TestHeader THeader;
·          
·                 [WebMethod]
·                 [SoapHeader("THeader")]
·                 public string TestSoapHeader()
·                 {
·          
·                     return "THeader Value: " + THeader.HeaderData;
·                 }
·             }
·          
·             public class TestHeader : SoapHeader
·             {
·                 public string HeaderData { get; set; }
·             }


#JQuery code in the webform page

<script type="text/javascript">
 
        function CallWebMethodWithHeader() {
 
            var soapXML = 
            "<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/soap/envelope/‘>" +
            "  <soap:Header>" +
                "<TestHeader xmlns=‘http://tempuri.org/‘>" +
                    "<HeaderData>111111111111</HeaderData>" +
                "</TestHeader>" +
            "</soap:Header>" +
            "<soap:Body>" +
                "<TestSoapHeader xmlns=‘http://tempuri.org/‘ />" +
            "</soap:Body>" +
            "</soap:Envelope>";
        
            $.ajax({
                url: "WebService4JQuery.asmx?op=TestSoapHeader",
                type: "POST",
                dataType: "xml",
                contentType:"text/xml; charset=utf-8",
                data: soapXML,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader(‘SOAPAction‘, ‘http://tempuri.org/TestSoapHeader‘);
 
                },
 
                success: function (data) {
                    alert("webmethod call success");
                },
                error: function (err) {
                    alert("webmethod call failed");
                }
 
            });
 
        }
    </script>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。