网上找的例子
js:
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/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) {
debugger
alert("webmethod call success");
},
error: function (err) {
debugger
alert("webmethod call failed");
}
});
}
cs:
public class WebService4JQuery : System.Web.Services.WebService
{
public TestHeader THeader;
[WebMethod]
[SoapHeader("THeader")]
public string TestSoapHeader()
{
return "THeader Value:11111111111";
}
}
public class TestHeader : SoapHeader
{
public string HeaderData { get; set; }
}
虽然调用成功了,但是返回的数据呢,好些是解析错误了!如图:
我看你ajax里 dataType 设置的是“xml”;但是你返回的数据格式明显不是xml;估计是这里的问题。
用jQuery SOAP插件
https://github.com/doedje/jquery.soap
多谢