我创建了一个非常简单的SOAP请求来访问由W3Schools托管的示例Web服务(下面的代码)。现在,执行脚本(通过按下按钮),响应是一个OPTIONS代码200 OK。
所以服务似乎已经开始,我在我的ajax请求中启用了“crossDomain”,但它不会发布实际数据。我该如何解决?我做了几天的研究,仍然无法得到我可以用于JavaScript的答案。请帮忙!
亲切的问候,jaySon
<html>
<head>
<title>SOAP JavaScript Client Test</title>
<script type="text/javascript" src="jquery-2.1.1.min.js" ></script>
<script type="text/javascript" >
function fnc_soap() {
var request =
'<?xml version="1.0" encoding="utf-8"?> ' +
'<soap12:Envelope ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" ' +
'soap12:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> ' +
'<soap12:Body> ' +
'<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"> ' +
'<Celsius>37</Celsius> ' +
'</CelsiusToFahrenheit> ' +
'</soap12:Body> ' +
'</soap12:Envelope>';
var url = "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit";
// var url = "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit";
jQuery.ajax({
type: "POST",
url: url,
data: request,
dataType: "xml",
crossDomain: true,
// contentType: "text/xml; charset=\"utf-8\"",
xhrFields: {
withCredentials: true
},
beforeSend: function(xhr) {
// xhr.setRequestHeader('Access-Control-Allow-Methods', 'OPTIONS, TRACE, GET, HEAD, POST');
xhr.withCredentials = true;
},
headers:
{
SOAPAction: "http://www.w3schools.com/webservices/CelsiusToFahrenheit",
// SOAPAction: "http://www.w3schools.com/webservices/tempconvert.asmx?CelsiusToFahrenheit"
"X-Requested-With": "XMLHttpRequest"
},
success: function (msg) {
alert("Works!");
},
error: function (msg) {
alert("Failed: " + msg.status + ": " + msg.statusText);
}
});
}
</script>
</head>
<body>
<form name="Demo" action="" method="post">
<div>
<input type="button" value="Soap" onclick="fnc_soap();" ></input>
</div>
</form>
</body>
<html>