Mic*_*ter 2 android web-services cordova
Phonegap船员,
我在使用android访问web服务时遇到问题.使用iOS访问它没有问题.随附的代码使用公共Web服务,因此如果您愿意,可以尝试使用代码.
在iOS上,我们得到一个xmlhttp.status == 200并返回数据.在Android上我们得到一个xmlhttp.status == 0.
我们正在使用cordova-1.8.1.jar
我们在res/xml/cordova.xml中设置了白名单,如下所示:
<access origin=".*"/>
我提起这件事是因为我怀疑我们的白名单不起作用.
这是代码:
function testweather(){
   var xhr= new XMLHttpRequest();
   xhr.onreadystatechange = function(){
      alert(xhr.readyState);
      if(xhr.readyState == 4){
         if(xhr.status == 200){
            $( "#result" ).append( xhr.responseText );
         }
         else{
            alert("can't get response. a.status:"+xhr.status);
         }
      }
   }
var url = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php";
xhr.open("POST", url,true);
xhr.setRequestHeader("SOAPAction",   "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDayLatLonList");
xhr.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
xhr.setRequestHeader("Content-Length", 1536);
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
xhr.setRequestHeader("User-Agent", "IBM Web Services Explorer");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Pragma", "no-cache");
xhr.setRequestHeader("Connection", "close");
var soapEnv = '' +
    '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">' +
    '   <soapenv:Header/>' +
    '   <soapenv:Body>' +
    '      <ndf:NDFDgenByDayLatLonList soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
    '         <listLatLon xsi:type="dwml:listLatLonType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">35.4,-97.6</listLatLon>' +
    '         <startDate xsi:type="xsd:date">2012-06-27</startDate>' +
    '         <numDays xsi:type="xsd:integer">3</numDays>' +
    '         <Unit xsi:type="dwml:unitType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">e</Unit>' +
    '         <format xsi:type="dwml:formatType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">24 hourly</format>' +
    '      </ndf:NDFDgenByDayLatLonList>' +
    '   </soapenv:Body>' +
    '</soapenv:Envelope>';
xhr.send( soapEnv );
}
有时,当您从文件协议执行AJAX请求时,您将获得状态0但实际上是200.只需更改您是否:
if(xhr.status == 200 || xhr.status == 0)
你应该好好去.
这是我在PhoneGap上使用AJAX写的一篇博客文章.
http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html
更新:显然Android 2.x中存在一个错误,其中设置"Content-length"标题会导致您描述的问题.看起来这个bug已在Android 4.0.3中得到修复.所以尝试在4.0.3模拟器中未经修改的此代码,它应该工作,然后回到2.x并删除Content-length标头,看看它是否也能正常工作.
| 归档时间: | 
 | 
| 查看次数: | 4548 次 | 
| 最近记录: |