
SOAP 簡單物件存取協定
- Simple Object Access Protocol
- 一種標準化的通訊規範
- SOAP協議= HTTP協議+ XML數據格式
- 以XML為基礎的通訊協定,其作用是編譯網路服務所需的要求或回應後,再將編譯後的訊息送出到網路,簡單來說就是應用程式和用戶之間傳輸資料的一種機制
語法規則

Example
<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:vis="http://vision.forestry.xxx/">
   <soapenv:Header/>
   <soapenv:Body>
      <vis:GetVer/>
   </soapenv:Body>
</soapenv:Envelope>
測試工具:SoapUI
jQuery Soap
- 直接使用jQuery來完成SOAP所做的Web Services
- jquery.soap.js:download
parameter 常用參數
- url (String)
- 指定要進行呼叫的後端位址
- 可以為絕對位址、相對位址
 
- method (String)
- envAttributes (Object)
- Set additional attributes (like namespaces) on the soap:Envelope node
- 例如:{ "xmlns:vis": "http://vision.forestry.xxx/" }
 
- appendMethodToURL (Boolean)
- data
- 傳送至Server的資料
- Type
var xml =
    ['<requestNode>',
        '<name>Remy Blom</name>',
        '<msg>Hi!</msg>',
    '</requestNode>'];
$.soap({
    data: xml.join('')
});
 
- json:格式為Key/Value,可節省很多程式碼 (推薦使用)
 $.soap({
    method: 'requestNode',
    data: {
        name: 'Remy Blom',
        msg: 'Hi!'
    }
});
 
- more detail
 
- success
其他參數
參數 data 詳細解說
- 原始 saop xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
	<soap:Body>
		<requestNode>
			<name>Remy Blom</name>
			<msg>Hi!</msg>
		</requestNode>
	</soap:Body>
</soap:Envelope>
- 寫法一:data(string)
- 直接將整個 soap 的 body 部分的 xml 丟進 data
 var xml =
    ['<requestNode>',
        '<name>Remy Blom</name>',
        '<msg>Hi!</msg>',
    '</requestNode>'];
$.soap({
    data: xml.join('')
});
 
- 寫法二:data(json object) + method
$.soap({
    method: 'requestNode',
    data: {
        name: 'Remy Blom',
        msg: 'Hi!'
    }
});
 
Example1:有 data
jQuery
function startCamera(view) {
    $.soap({
        url: "/cgi-bin/ctrl_service",   
        method: "vis:StartView",    
        data: { view: view },   
        envAttributes: { "xmlns:vis": "http://vision.forestry.xxx/" },   
        appendMethodToURL: false,   
        success: function(response) {
            
            console.log(response);
        }
    });
}
生成的 SOAP 程式碼
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vis="http://vision.forestry.xxx/">
    <soap:Body>
        <vis:StartView>
            <view>left</view>
        </vis:StartView>
    </soap:Body>
</soap:Envelope>
Example2:無 data
jQuery
function rematch(view) {
    $.soap({
        url: "/cgi-bin/ctrl_service",
        namespaceQualifier: "vis",
	    namespaceURL: "http://vision.forestry.xxx/",
        method: "Rematch",
        data: {},
        appendMethodToURL: false,
        success: function(response) {
            console.log(response)
        }
    });
}
生成的 SOAP 程式碼
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <vis:Rematch xmlns:vis="http://vision.forestry.xxx/"></vis:Rematch>
    </soap:Body>
</soap:Envelope>
討論
- Q:為何 Example1 不能使用 Example2 的方式?
- 以下為套用 Example2 的寫法後生成的 SOAP
- 可以發現 data tag <view>的部分變成了vis:view→ 故造成錯誤
- namespaceQualifier: "vis"不僅將 method 加上 namespaces,連 data 也跟著加上 namespaces,故造成錯誤
 
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <vis:StartView xmlns:vis="http://vision.forestry.xxx/">
            <vis:view>left</vis:view>
        </vis:StartView>
    </soap:Body>
</soap:Envelope>
Reference
- 網路通訊協定 - SOAP:
 https://www.ctimes.com.tw/culture/showbox-tw.asp?o=HJN84855CB0CU-0PT1
- SoapUI 官網:https://www.soapui.org/
- jQuery Soap 官網:https://github.com/doedje/jquery.soap
 
 
沒有留言:
張貼留言