我正在尝试使用 jQuery Ajax 调用 asmx 服务。
POST /YderWS.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://scandihealth.com/iwebservices/HentKommuner"
<?xml version="1.0" encoding="utf-8"?>
<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>
<AuthHeader xmlns="http://scandihealth.com/iwebservices/">
<PartnerID>string</PartnerID>
<SubPartnerID>string</SubPartnerID>
<SubPartnerType>string</SubPartnerType>
</AuthHeader>
</soap:Header>
<soap:Body>
<HentKommuner xmlns="http://scandihealth.com/iwebservices/" />
</soap:Body>
</soap:Envelope>
上面是我需要发送到服务的 SOAP 1.1 请求。我使用下面的调用来设置自定义肥皂头。但我的请求失败了。有人可以帮我调试下面的代码并让我知道我需要做什么吗?
var authHeader = "<PartnerID>SCTEST001</PartnerID> <SubPartnerID>001</SubPartnerID> <SubPartnerType>S</SubPartnerType>";
//Call the page method
$.ajax({
type: "GET",
url: servicename + "/" + functionName,
beforeSend: function (xhr) {
xhr.setRequestHeader('AuthHeader', authHeader);
},
success: successFn,
error: errorFn
});
编辑 *如果需要其他信息来回答此问题,请告诉我。*
最佳答案
jQuery.ajax()
向任何类型的“Web 服务”发出通用 HTTP 请求,而不仅仅是 .NET Web 服务。您需要添加 SOAPAction 请求 header 并将整个 SOAP 信封作为 POST 数据传递:
$.ajax({
type: 'POST',
url: servicename + "/" + functionName,
contentType: 'text/xml; charset=utf-8',
headers: {
SOAPAction: 'http://scandihealth.com/iwebservices/HentKommuner'
},
data: '<?xml version="1.0" encoding="utf-8"?><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><AuthHeader xmlns="http://scandihealth.com/iwebservices/"><PartnerID>string</PartnerID><SubPartnerID>string</SubPartnerID><SubPartnerType>string</SubPartnerType></AuthHeader></soap:Header><soap:Body><HentKommuner xmlns="http://scandihealth.com/iwebservices/" /></soap:Body></soap:Envelope>',
success: successFn,
error: errorFn
});
如果您使用的是 jQuery < 1.5,则需要使用 beforeSend
来设置 SOAPAction 请求 header 。
您可以在 http://api.jquery.com/jQuery.ajax/ 找到 jQuery.ajax()
的文档。 .
关于jquery - 使用 jQuery Ajax 发送 SOAP 信封的自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933782/