Javascript 中的簡單 SOAP 實作
使用正確的方法,在 Javascript 中建立 SOAP 客戶端可以非常簡單。本文探討最簡單的 SOAP 用戶端範例,確保功能並滿足多個條件。
實作客戶端
以下程式碼提供了一個用 Javascript 寫的精簡 SOAP 用戶端:
function soap() { let xmlhttp = new XMLHttpRequest(); xmlhttp.open('POST', 'https://somesoapurl.com/', true); // build SOAP request let sr = `<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <api:some_api_call soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <username xsi:type="xsd:string">login_username</username> <password xsi:type="xsd:string">password</password> </api:some_api_call> </soapenv:Body> </soapenv:Envelope>`; xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { alert(xmlhttp.responseText); // alert('done. use firebug/console to see network response'); } } } // Send the POST request xmlhttp.setRequestHeader('Content-Type', 'text/xml'); xmlhttp.send(sr); // send request // ... }
此程式碼範例了以下內容要點:
用法:
要使用客戶端,請在HTML 文件中呼叫soap() 函數。它將發送請求並顯示回應。
以上是如何用 Javascript 建立一個簡單的 SOAP 客戶端?的詳細內容。更多資訊請關注PHP中文網其他相關文章!