2013-07-26 40 views
1

我需要在我的網站上使用JS或JQuery來使用Rest API XML響應。有人可以爲此提供代碼。提前致謝。需要使用Javascript/JQuery來使用Rest API XML響應

以下是REST API調用來獲取下面顯示的XML響應:

/RESTAPI/VC /板/ ID/{板ID} /訂閱/全球/浮點/線程

XML響應

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<response status="success"> 
    <subscriptions> 
    <subscription type="subscription" href="/subscriptions/global/id/321"> 
     <id type="int">321</id> 
     <target type="thread" href="/threads/id/4849"> 
     <id type="int">4849</id> 
     <title type="string" null="true"/> 
     <messages> 
      <count type="int">1</count> 
      <topic type="message" href="/messages/id/4849"> 
      <id type="int">4849</id> 
      <read_only type="boolean">false</read_only> 
      <parent type="message" null="true"/> 
      <teaser type="string"></teaser> 
      <views> 
       <count type="int">2</count> 
      </views> 
      <subject type="string">Cannot download Pinball Arcade vita</subject> 
      <deleted type="boolean">false</deleted> 
      <author type="user" href="https://stackoverflow.com/users/id/149"> 
       <login type="string">psforums</login> 
      </author> 
      <thread type="thread" href="/threads/id/4849"/> 
      <board_id type="int">1</board_id> 
      <message_rating type="float">0.0</message_rating> 
      <last_edit_author type="user" href="https://stackoverflow.com/users/id/149"> 
       <login type="string">psforums</login> 
      </last_edit_author> 
      <kudos> 
       <count type="int">0</count> 
      </kudos> 
      <last_edit_time type="date_time">2013-07-24T16:28:44+00:00</last_edit_time> 
      <post_time type="date_time">2013-07-24T16:28:44+00:00</post_time> 
      <labels/> 
      <root type="message" href="/messages/id/4849"/> 
      <board type="board" href="/boards/id/22034"/> 
      </topic> 
      <read> 
      <count type="int">1</count> 
      </read> 
      <linear> 
      <message type="message" href="/messages/id/4849"> 
       <id type="int">4849</id> 
       <read_only type="boolean">false</read_only> 
       <parent type="message" null="true"/> 
       <teaser type="string"></teaser> 
       <views> 
       <count type="int">2</count> 
       </views> 
       <subject type="string">Cannot download Pinball Arcade vita</subject> 
       <deleted type="boolean">false</deleted> 
       <author type="user" href="https://stackoverflow.com/users/id/149"> 
       <login type="string">psforums</login> 
       </author> 
       <thread type="thread" href="/threads/id/4849"/> 
       <board_id type="int">1</board_id> 
       <message_rating type="float">0.0</message_rating> 
       <last_edit_author type="user" href="https://stackoverflow.com/users/id/149"> 
       <login type="string">psforums</login> 
       </last_edit_author> 
       <kudos> 
       <count type="int">0</count> 
       </kudos> 
       <last_edit_time type="date_time">2013-07-24T16:28:44+00:00</last_edit_time> 
       <post_time type="date_time">2013-07-24T16:28:44+00:00</post_time> 
       <labels/> 
       <root type="message" href="/messages/id/4849"/> 
       <board type="board" href="/boards/id/22034"/> 
      </message> 
      </linear> 
     </messages> 
     <board type="board" href="/boards/id/22034"/> 
     <solutions/> 
     <interaction_style type="string">board</interaction_style> 
     </target> 
     <target_type type="subscription_target_type">thread</target_type> 
     <user type="user" null="true"/> 
     <subscription_type type="subscription_type">float</subscription_type> 
    </subscription> 
    </subscriptions> 
</response> 

回答

1
$(function() { 
$.ajax({ 
    type: "POST", 
    url: "URL", 
    contentType: "text/xml; charset=utf-8", 
    data: "DATA", 
    success: function (response) { 
     $('#result').html('success:'); 
     $(response).find("string").each(function() { 
      $('#result').append('<br />'+$(this).text()); 
     }); 
    }, 
    error: function (response) { 
     $('#result').html('failure:<br />' + response.responseText); 
    } 
}); 

});

希望這將工作

我的回答是這樣的

<string>tag 1</string> 
    <string>tag 2</string> 
<string>tag 3</string> 
</ArrayOfString> 
+0

你的代碼工作完美,謝謝。我現在遇到另一個阻止程序了,是否有一種方法可以在JQuery中使用,我可以在/ response/subscriptions/subscription/target/messages/topic/**上僅獲取節點的數據。** SUBJECT ** NOT NOT Subject節點at/response/subscriptions/subscription/target/messages/linear/message/*主題* –

+0

@VarunLuthra你能解釋一下嗎? – Backtrack