javascript - 需要使用 Javascript/JQuery 使用 Rest API XML 响应

标签 javascript jquery

我需要使用 JS 或 JQuery 在我的网站上使用 Rest API XML 响应。有人可以提供这方面的代码吗?提前致谢。

以下是获取下面显示的 XML 响应的 REST API 调用:

/restapi/vc/boards/id/{board id}/subscriptions/global/float/thread

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="/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="/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="/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="/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>

最佳答案

  $(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>

关于javascript - 需要使用 Javascript/JQuery 使用 Rest API XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17872729/

相关文章:

javascript - JSON数组Tabulator js表

jquery - 淡出 CSS3 动画

javascript - 如何使返回顶部链接快速滚动到顶部而不是立即跳转

javascript - 5 秒后加载

jquery - 提交表单后保留当前选项卡

javascript - 无法使用 iOS 向下滚动 100% 高度部分

javascript - ajax 请求使页面滞后

java - 如何在 Spring MVC (3.0) 应用程序中处理 Ajax.Request 调用?

javascript - 单击事件时从 jquery 触发 vue 方法

javascript - 模式中的关闭链接正在关闭成功消息 div 的所有实例