使用PHP的EmberJS API问题 [英] EmberJS API questions using PHP
问题描述
我正在使用Laravel / PHP输出一些用于Ember的JSON来接收....这里有几件事。
I'm using Laravel/PHP to output some JSON for Ember to pick up.... a couple of things here.
首先,我的PHP看起来像这是(还有另一种方式发送数据)
First, my PHP looks like this (is there another way to send the data)
return Response::json([
'articles' => $articles->toArray()
], $statusCode);
这是我以前做的。
foreach($articles as $article) {
$response['articles'][] = [
'id' => $article->id,
'body' => $article->body,
'title' => $article->title
];
}
return Response::json([
'articles' => $articles->toArray()
], $statusCode);
第一个PHP代码段工作正常,但第二个没有。我收到Ember的资源类型的各种错误。
The first PHP snippet works fine, but the second does not. I get all kinds of errors about resource types by Ember.
下一个问题是Ember头。现在我正在使用 RESTAdapter
的所有工作,但是我应该使用 JSONAPIAdapter
?当我尝试使用 JSONAPIAdapter
和 JSONAPISerializer
时,我得到这个错误
Next question is for Ember heads. Right now I am getting everything working with RESTAdapter
but should I be using JSONAPIAdapter
instead? When I try to get it working with JSONAPIAdapter
and JSONAPISerializer
I get this error
一个或多个以下密钥必须存在:\data\,
\errors \ ,\meta
One or more of the following keys must be present: \"data\", \"errors\", \"meta
。我可以得到这个错误消失,但是我收到一个关于未定义类型或未知的错误资源。
. I can get that error to go away but then I get an error about an undefined type or an unknown resource.
推荐答案
它不是强制使用JSONAPIAdapter,但如果您可以控制API,那么您可以很好地使用它。 API响应应遵循格式( http://jsonapi.org/format/ )
Its not mandatory to use JSONAPIAdapter, but if you have control over the API then you can very well using it. API response should follow the format (http://jsonapi.org/format/)
单一资源对象的示例格式
Sample format for single resource object,
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
// ... this article's attributes
}
}
}
多个资源对象的示例格式
Sample format for multiple resource objects,
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
}, {
"type": "articles",
"id": "2",
"attributes": {
"title": "Rails is Omakase"
}
}]
}
这篇关于使用PHP的EmberJS API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!