PhoneGap应用程序调用RestFUL API

以Android平台为例。

假如API返回结果为XML格式的数据:

<?xml version="1.0" encoding="UTF-8" ?>
<deals>
	<deal id="857" slug="test_slug" city_id="1025">
            <deal_detail subject="testing deal">...</deal_detail>
            ...
        </deal>
        ...
</deals>

那么可以在main.js中定义如下函数,并在init即程序主页面加载时调用显示deal列表。

function get_deals() {   

$.ajax({
        url: "http://sandbox.jigoapps.com/deals.xml?country=au&lang=en-us",
        data: {},
        dataType: "xml",
        headers: {"Accept": "application/xml"},
        success: function(response) {
          var i=0;
          $(response).find("deal").each(function()
          {
              console.log("deal id:"+$(this).attr("id")); //do more for listing deals...

          });      
        },
        error: function(request, status, error) {
          console.log("Error status " + status);
          console.log("Error request status text: " + request.statusText);
          console.log("Error request status: " + request.status);
          console.log("Error request response text: " + request.responseText);
          console.log("Error response header: " + request.getAllResponseHeaders());
          $("#error").html(status);
        }

}


regards,

iefreer


你可能感兴趣的:(function,api,PhoneGap,encoding,testing,slug)