2013-03-27 49 views
0

我想調用API並以XML格式獲得結果 我的當前代碼如下所示。 當我直接提供XML文件,然後我的代碼按預期方式運行,但是當我提到API url以獲得相同的XML時,則沒有錯誤,但調用API不會發生。不從API調用獲取xml

我想用AJAX我當前的代碼來做到這一點是如下:

<script type="text/javascript"> 
var searchq = $("#txtTag").val(); 
var twitpicURL = 'http://twitpic.com/show/thumb/'; 
$(document).ready(function() { 
    $("#btnGetData").click(function() { 
     $.ajax({ 
      type: "GET", 
      /* define url of xml file to parse */ 
      //url: "show.xml", 
      url: "http://api.twitpic.com/2/tags/show.xml?tag=" + searchq, 
      dataType: "xml", 
      success: parseXml 
     }); 
    }); 

    function parseXml(xml) 
    /* this is where the xml file is parsed and converted into a HTML output */ 
    { 
     alert(xml); 
     //for each item node in the xml file 
     var html = ''; 
     $(xml).find("image").each(function() { 
      var imageId = $(this).find("short_id").text(); 
      var imagesrc = twitpicURL + imageId; 

      html += '<tr class="menu_item">'; 
      html += '<td class="h-menu">' + "<img src='" + imagesrc + "'/>" + '</td>'; 
      html += '<td class="h-menu">'; 
      html += '<div>' + $(this).find("id").text() + '</div>'; 
      html += '<div>' + $(this).find("short_id").text() + '</div>'; 
      html += '<div>' + $(this).find("type").text() + '</div>'; 
      html += '<div>' + $(this).find("timestamp").text() + '</div>'; 
      html += '<div>' + $(this).find("message").text() + '</div>'; 
      html += '</td>'; 
      html += '</tr>'; 

     }); 
     $('#tblTwitpic').append($(html)); 
     //end for each 
     //end function 
    } 
    //$("#tweets").append("</table>"); 
}); 

我的HTML看起來象下面這樣:

<body> 
<input id="btnGetData" type="button" value="Twitter Get Tweets" /> 
<input type="text" id="txtTag" /> 
<div id="tweets"> 
<table id="tblTwitpic" border='1'></table> 
</div> 

我不是得到任何錯誤,但沒有發生對這個API的調用。

在此先感謝; 阿布舍克A.夏爾馬

回答

0
url: "http://api.twitpic.com/2/tags/show.xml?tag=" + searchq, 

只是改變了輸出.XML到JSONP。 仍然我不知道爲什麼這不解析xml的對象。