【问题标题】:Angular - REST Service XML Links with $ResourceAngular - 带有 $Resource 的 REST 服务 XML 链接
【发布时间】:2015-03-01 21:30:16
【问题描述】:

我在后端使用 Angular 和 SilverStripe。我将这个模块 https://github.com/silverstripe/silverstripe-restfulserver 用于我的 REST API,而不是自己构建它。

当 API 返回一个具有 many_many 关系的列表时,它看起来像

<Recipe href="http://localhost/groceryList/api/v1/Recipe/2.xml">
<Title>Chilli</Title>
<ID>2</ID>
<Ingredients linktype="many_many" href="http://localhost/groceryList/api/v1/Recipe/2/Ingredients.xml">
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/5.xml" id="5"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/6.xml" id="6"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/7.xml" id="7"/>
</Ingredients>
</Recipe>

我的问题是 2 部分。

  1. 为什么成分的集合是一堆链接?我希望它会包含每种成分的名称。这就是 REST 的工作方式吗?

  2. 使用 Angular $Resource,如何使用 ng-repeat 轻松让成分循环通过?

【问题讨论】:

标签: angularjs rest silverstripe


【解决方案1】:

首先您需要使用https://code.google.com/p/x2js/https://github.com/johngeorgewright/angular-xml 将您的XML 转换为JSON,因此您需要一个HttpInterceptor 来转换来自服务器的所有请求和响应。

我不知道为什么您的响应中没有每种成分的名称,我认为您需要更改 API 返回的响应,但我不知道 SilverStripe 是如何工作的。

【讨论】:

  • 我可以很容易地将响应转换为 json,所以这不是问题。改变响应可能是最好的解决方案。我正在考虑这样做,但我很困惑为什么最初的开发人员会这样编程。我会等待其他一些答案,但如果更改回复是最好的方法,我会接受你的答案。
  • 可能是因为它们是其他使用 API 的应用程序?如果 API 可以在 JSON 中接收和响应,那么与 AngularJS 一起使用会更容易。许多旧服务仍然使用带有 XML 的 SOAP,主要原因是它们与其他服务互操作。 blog.feedly.com/2009/03/03/jsonrest-vs-xmlsoapblog.westmonroepartners.com/…
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多