寫這個花了我很多時間去研究,不管JQUERY如何寫AJAX都失敗,後來才發現是跨網域的問題…
> JQuery
$(document).ready(function () 
{
    Hello();
    function Hello() 
    { 
        //這個XML格式可以在Web Service的HELP裡看到
        
var soapXMLMessage =
'<?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>
… *** 這邊通常是你定義的SOAP Header XML *** …
</soap:Header>
<soap:Body>
… *** 這邊通常是你定義的Web Service Method XML *** …
</soap:Body>
</soap:Envelope>';
var soapXMLMessage =
'<?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>
… *** 這邊通常是你定義的SOAP Header XML *** …
</soap:Header>
<soap:Body>
… *** 這邊通常是你定義的Web Service Method XML *** …
</soap:Body>
</soap:Envelope>';
        //這邊放你Web Service的Url
        var soapUrl = 'https://localhost:123/WebServiceUrl.asmx'; 
        $.ajax({                            
             type:"POST",
             url: soapUrl,
             data: soapXMLMessage,
             dataType: "xml",
             contentType: "text/xml; charset=utf-8",                            
             success: function OnSuccess(data, status)
             {
                alert(data.firstChild.textContent);
             },
             error: function OnError(request, status)
             {
                alert(request.status + ' ' + request.statusText);
             }
        });
    }
});
如果有跨網域的問題,例如Chrome瀏覽器都有這問題,還要加上這個設定:
> Web Service Config
<system.webServer>
    <httpProtocol>            
        <customHeaders>
<!--允許哪個 Client Url 進行 ajax post-->
<!--允許哪個 Client Url 進行 ajax post-->
        <add name="Access-Control-Allow-Headers" value="origin, content-type" />
        </customHeaders>
    </httpProtocol>
</system.webServer>
留言
張貼留言