【发布时间】:2016-10-16 21:55:12
【问题描述】:
我一直在尝试将 Angular2 示例“英雄之旅”连接为前端部分,将 Yii2 框架连接为后端 yii2中的控制器
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class HeroesController extends ActiveController
{
public $modelClass = 'app\models\Heroes';
public function behaviors()
{
return
yii\helpers\ArrayHelper::merge(parent::behaviors(), [
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
],
]);
}
}
结果(http://server.local/heroes):
<response>
<item>
<id>11</id>
<name>Mr. Nice</name>
<title>князь</title>
</item>
<item>
<id>12</id>
<name>Narco</name>
<title>граф</title>
</item>
<item>
<id>13</id>
<name>Bombasto</name>
<title>барон</title>
</item>
curl H 'Content-Type': 'application/json' 'http://server.local/heroes' 工作正常,我得到 JSON
但我无法在 Angular2 中接收到这个。 http.get with options Content-Type': 'application/json
export class HeroService {
private headers = new Headers({'Content-Type': 'application/json'});
private options = new RequestOptions({ headers: this.headers});
private heroesUrl ='http://server.local/heroes';// 'app/heroes'; // URL to web api
constructor(private http: Http) { }
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl, this.options
)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
但我得到一个空英雄[]
【问题讨论】:
-
浏览器控制台中显示的任何错误?您是否还可以检查请求是否已从开发工具的网络选项卡正确发送?
-
Chrome 控制台中没有错误