2012-06-07 32 views
2

我想使用jQuery Ajax調用asmx服務。使用jQuery Ajax發送肥皂信封的自定義標題

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 
}); 

編輯 * 請讓我知道,如果需要更多的信息來回答這個問題。 *

+0

你得到什麼錯誤原因?它可能很容易成爲一個跨域問題。 –

回答

3

jQuery.ajax()針對任何類型的「Web服務」發佈泛型HTTP請求,而不僅僅是.NET Web服務。你要添加的SOAPAction請求頭和傳遞整個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請求頭。

你可以找到jQuery.ajax()的文件在http://api.jquery.com/jQuery.ajax/

+1

只需注意,「標題」參數直到1.5才被添加,因此請確保您是合理的最新版本! –

0

好像你錯過添加這些:

contentType: 'text/xml; charset=utf-8', 
dataType: 'xml' 

添加這兩條線後,它工作正常,我有調試它。