2013-08-30 82 views
0

我正在嘗試從使用jQuery使用Ajax如下XML文件的XML信息的外部XML文件:檢索使用jQuery和AJAX

function getWeatherXML() { 
     // still subjected to CORS issue ... 
     alert('Getting weather information...'); 
     $.support.cors = true; 
     $.ajax({ 
      url: "http://www.weather.gov.sg/wip/pp/rndops/web/rss/3Day_RSS.xml? callback=?", 
      type: "GET", 
      dataType: "xml", //type of data for response 
      crossDomain: "true" , 
      jsonpCallback : "myResponse", 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
       alert(textStatus); 
       alert(errorThrown); 
       } 
     }); 
    } 

    function myResponse(data) { 
     alert(data); 
    } 

不過,我仍然受到跨源資源共享問題,反正我可以解決這個問題嗎?我正在測試我的Android設備,因此我的起源將被聲明爲空。原因是因爲我的webView.loadurl以file:///

開頭謝謝。

回答

0

取決於你如何和WebView您的本機應用程序Android綁在一起,你可以使用Java得到XML,然後將它交給你的Javascript。對於這樣的任務,我會建議droidQuery,因爲這將是簡單的端口從現有代碼:

public void getWeatherXML() { 
    $.with(this).alert("Getting weather information..."); 
    //You can either set the type to `XML`, then parse it with Java, or you can just set the type to String, and hand it off to your Javascript 
    $.ajax(new AjaxOptions().url("http://www.weather.gov.sg/wip/pp/rndops/web/rss/3Day_RSS.xml") 
          .context(this) 
          .type("GET") 
          .dataType("text") 
          .error(new Function() { 
           @Override 
           public void invoke($ droidQuery, Object... params) { 
            AjaxError error = (AjaxError) params[0]; 
            droidQuery.alert(error.reason); 
            droidQuery.alert("Error " + error.status + ": " + error.reason); 
           } 
          }) 
          .success(new Function() { 
           @Override 
           public void invoke($ droidQuery, Object... params) { 
            String xml = (String) params[0]; 
            //TODO hand xml String to your Javascript 
           } 
          })); 
} 
+0

。另一方面,這將意味着我不能因爲要測試這一點在任何蘋果設備droidQuery僅在android平臺上可用?原因是我正試圖讓這個混合應用程序.. 任何其他方式來處理這種情況? – CodeGuru