向RESTful web 服务发送XML请求示例

做好了RESTful web 服务后,可以通过很多种方式向服务发起请求,本文仅介绍最简单的XMLHttpRequest发起请求方式。

客户端脚本如下:

<script type="text/javascript">
 
        var xmlHttp 
= null; var url = ""; var content = "";
 
        function PostXML() {
            xmlHttp.onreadystatechange 
= show;
            xmlHttp.open(
"POST", url);
            xmlHttp.setRequestHeader(
"Content-Type""application/xml");
            xmlHttp.send(content);
        }
        function show() {
            
if (xmlHttp.readyState == 4) {
                
if (xmlHttp.status == 200) {
                    $(
"#txtResult").val(xmlHttp.responseText);
                }
                
else {
                    $(
"#txtResult").val(xmlHttp.status.toString() + ":" + xmlHttp.statusText);
                }
            }
        }
 
        window.onload 
= function() {
           
if (window.XMLHttpRequest) {
                xmlHttp 
= new XMLHttpRequest();
            }
            
else if (window.ActiveXObject) {
                xmlHttp 
= new ActiveXObject("Microsoft.XMLHttp");
            }
            url 
= $("#txtURL").val();
            content 
= $("#txtRequest").val();
        }
    
</script>

请求格式为:

<?xml version="1.0"?>
<VisitCostRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IndicationCode="indic" PatientStatus="1" TrialPhase="2" VisitCount="10">
  
<Country>
    
<Code>ddd</Code>
  
</Country>
</VisitCostRequest>

最终,通过页面控件调用PostXML方法即可。

文章出处:www.cnblogs.com/jizhong

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/jizhong/p/2032869.html