2010-02-26 70 views
0

好吧,以前我问... SOAP原型AJAX SOAPAction头问题(不能超链接是不幸的是,“2”的链接没有足够的代表......见下文)JavaScript的SOAP XMLHttpRequest的问题移动

从来没有制定出。我认为它与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和状态== 200(不是我检查的)。我通常不需要用勺子喂食,但我不明白为什么我没有从听众那里得到我需要的值,或者为什么它从来没有被调用过。另外,并不是说这个问题确实很重要,但我正在批准Firefox上的请求是跨域的,但它确实适用于移动设备,因此呼叫不需要跨域确认,它会自动执行此操作。

我希望有人可以看看这个,看看我忽略了什么。谢谢!

回答

1

onreadystatechange只会针对服务器的异步请求调用,您的代码正在发送同步请求。

将打开调用的第三个参数设置为true(或者将第三个参数设置为默认为true)。

request.open("POST", "http://iaspub.epa.gov/uvindexalert/services/UVIndexAlertPort?wsdl", true); 

http://msdn.microsoft.com/en-us/library/ms536648(VS.85).aspx

+0

人!非常感谢。绞尽脑汁!我现在可以继续我的生活! – 2010-02-26 04:03:02

+0

很高兴帮助:) – Jonathan 2010-02-26 04:08:19