2010-02-26 66 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