javascript SOAP xmlhttprequest 问题移动
好吧,所以之前我问过... SOAP 原型 AJAX SOAPAction 标头问题(不幸的是,无法超链接它,“2”链接没有足够的代表......见下文)
但从未解决。我认为这与Prototype有关,它会返回0作为onSuccess。我无法弄清楚内容类型 utf-8 格式。现在,如果我回到直接的 javascript 并使用 xmlhttprequest
<html xmlns="http://www.w3.org/1999/xhtml">
function getUVIndex() {
// In Firefox, we must ask the user to grant the privileges we need to run.
// We need special privileges because we're talking to a web server other
// than the one that served the document that contains this script. UniversalXPConnect
// allows us to make an XMLHttpRequest to the server, and
// UniversalBrowserRead allows us to look at its response.
// In IE, the user must instead enable "Access data sources across domains"
// in the Tools->Internet Options->Security dialog.
if (typeof netscape != "undefined") {
netscape.security.PrivilegeManager.
enablePrivilege("UniversalXPConnect UniversalBrowserRead");
}
// Create an XMLHttpRequest to issue the SOAP request. This is a utility
// function defined in the last chapter.
var request = new XMLHttpRequest();
// We're going to be POSTing to this URL and want a synchronous response
request.open("POST", "http://iaspub.epa.gov/uvindexalert/services/UVIndexAlertPort?wsdl", false);
request.onreadystatechange=function() {
if (request.readyState==4) {
var index = request.responseXML.getElementByTagName('index')[0].firstChild.data;
alert(request.responseText);
}
}
// Set some headers: the body of this POST request is XML
request.setRequestHeader("Content-Type", "text/xml");
// This header is a required part of the SOAP protocol
request.setRequestHeader("SOAPAction", '""');
// Now send an XML-formatted SOAP request to the server
request.send(
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"' +
' xmlns:tns="urn:uvindexalert" xmlns:types="urn:uvindexalert/encodedTypes"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
' <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
' <tns:getUVIndexAlertByZipCode>' +
' <in0 xsi:type="xsd:string">12306</in0>' +
' </tns:getUVIndexAlertByZipCode>' +
' </soap:Body>' +
'</soap:Envelope>'
);
// If we got an HTTP error, throw an exception
if (request.status != 200) throw request.statusText;
//return request.responseXML.childNodes[0].childNodes[1].childNodes[3].childNodes[5].textContent;
}
getUVIndex();
</script>
这永远不会调用 onreadystatechange。如果您取消注释 返回 request.responseXML.childNodes[0].childNodes[1].childNodes[3].childNodes[5].textContent;
它将检索所需的值,如果您在 Firebug 中,您将看到 readyState == 4 和 status == 200 (不是我检查的)。我通常不需要用勺子喂食,但我只是不明白为什么我没有从听众那里得到我需要的值,或者为什么它永远不会被调用。另外,这并不重要,但我批准 Firefox 上的跨域请求,它确实适用于移动设备,因此调用不需要跨域确认,它会自动执行此操作。
我希望有人能看看这个并看看我忽略了什么。谢谢!
All right, so previously I asked...
SOAP Prototype AJAX SOAPAction Header Question (can't hyperlink it unfortunately, not enough rep for "2" links... see below)
Which never worked out. I think it has something to do with Prototype, it will return a 0 as onSuccess. I can't figure out the Content-type utf-8 formatting. Now if I go back to straight javascript and use xmlhttprequest
<html xmlns="http://www.w3.org/1999/xhtml">
function getUVIndex() {
// In Firefox, we must ask the user to grant the privileges we need to run.
// We need special privileges because we're talking to a web server other
// than the one that served the document that contains this script. UniversalXPConnect
// allows us to make an XMLHttpRequest to the server, and
// UniversalBrowserRead allows us to look at its response.
// In IE, the user must instead enable "Access data sources across domains"
// in the Tools->Internet Options->Security dialog.
if (typeof netscape != "undefined") {
netscape.security.PrivilegeManager.
enablePrivilege("UniversalXPConnect UniversalBrowserRead");
}
// Create an XMLHttpRequest to issue the SOAP request. This is a utility
// function defined in the last chapter.
var request = new XMLHttpRequest();
// We're going to be POSTing to this URL and want a synchronous response
request.open("POST", "http://iaspub.epa.gov/uvindexalert/services/UVIndexAlertPort?wsdl", false);
request.onreadystatechange=function() {
if (request.readyState==4) {
var index = request.responseXML.getElementByTagName('index')[0].firstChild.data;
alert(request.responseText);
}
}
// Set some headers: the body of this POST request is XML
request.setRequestHeader("Content-Type", "text/xml");
// This header is a required part of the SOAP protocol
request.setRequestHeader("SOAPAction", '""');
// Now send an XML-formatted SOAP request to the server
request.send(
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"' +
' xmlns:tns="urn:uvindexalert" xmlns:types="urn:uvindexalert/encodedTypes"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
' <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
' <tns:getUVIndexAlertByZipCode>' +
' <in0 xsi:type="xsd:string">12306</in0>' +
' </tns:getUVIndexAlertByZipCode>' +
' </soap:Body>' +
'</soap:Envelope>'
);
// If we got an HTTP error, throw an exception
if (request.status != 200) throw request.statusText;
//return request.responseXML.childNodes[0].childNodes[1].childNodes[3].childNodes[5].textContent;
}
getUVIndex();
</script>
This never calls the onreadystatechange. If you uncomment the
return request.responseXML.childNodes[0].childNodes[1].childNodes[3].childNodes[5].textContent;
It will retrieve the value needed and if you are in Firebug you will see the readyState == 4 and status == 200 (not that I check for that). I usually never need to be spoon fed but I just don't understand why I am not getting the values back I need from the listener, or why it is never called. Also, not that this should matter really but I am approving the request on Firefox to be cross-domain, it is really for mobile so the call will not need to be have a confirmation of cross-domain, it will do that automatically.
I hope someone can look at this and see what I overlooked. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当向服务器发出异步请求时才会调用 onreadystatechange,您的代码正在发送同步请求。
将 open 调用的第三个参数设置为 true(或删除第三个参数,因为默认值为 true)。
http://msdn.microsoft.com/en-us /library/ms536648(VS.85).aspx
onreadystatechange will only be called for asynchronous requests to the server, your code is sending a synchronous request.
Set the third parameter on the open call to true (or remove the third parameter as default is true).
http://msdn.microsoft.com/en-us/library/ms536648(VS.85).aspx