【发布时间】:2016-06-20 19:39:52
【问题描述】:
我正在编写 AngularDart 教程的修改版本。我正在根据开发人员指南/HTTP 客户端中的示例添加服务器调用。我试图连接到同一台机器上的实际服务器,而不是内存服务。我已经在我的 Rails 应用程序中使用 HTTPRequest 让它在普通 Dart 中工作。在我的新 AngularDart 独立客户端中,出现错误:
TypeError: this.get$BrowserClient is not a function
我怀疑这可能会发生,因为我从未在 main.dart 中创建内存服务器。如果是这样,与真实服务器对话的正确代码是什么?
我的服务等级:
import 'dart:async';
import 'dart:convert';
import 'package:angular2/core.dart';
import 'artist.dart';
import 'package:http/browser_client.dart';
import 'package:http/http.dart';
@Injectable()
class ArtistService {
static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server
final BrowserClient _http;
ArtistService(this._http);
Future<List<Artist>> getArtists() async {
try {
final response = await _http.get(_artistsUrl,
headers: {'Accept': 'application/json'});
final artists = _extractData(response)
.map((value) => new Artist.fromJson(value))
.toList();
return artists;
} catch (e) {
throw _handleError(e);
}
}
...
我的 main.dart 是:
import 'package:angular2/platform/browser.dart';
import 'package:jazzcat/app_component.dart';
main() {
bootstrap(AppComponent);
}
例子有:
import 'package:angular2/core.dart';
import 'package:angular2/platform/browser.dart';
import 'package:http/browser_client.dart';
import 'package:server_communication/app_component.dart';
import "package:server_communication/hero_data.dart";
void main() {
bootstrap(AppComponent, const [
// in-memory web api provider
const Provider(BrowserClient,
useFactory: HttpClientBackendServiceFactory, deps: const [])
// TODO: drop `deps` once fix lands for
// https://github.com/angular/angular/issues/5266
]);
}
B计划
我通过给出的链接尝试了这个:
import 'package:http/http.dart' as http;
@Injectable()
class ArtistService {
static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server
Future<List<Artist>> getArtists() async {
final response = await http.get(_artistsUrl, headers: {'Accept': 'application/json'});
final artists = _extractData(response)
.map((value) => new Artist.fromJson(value))
.toList();
return artists;
}
我得到了错误:
EXCEPTION: Unsupported operation: IOClient isn't supported on this platform.
C 计划
我修改了另一个项目的工作代码:
import 'dart:html';
@Injectable()
class ArtistService {
static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server
Map headers = {'Accept': 'application/json'};
Future<List<Artist>> getArtists() async {
final response = await HttpRequest.request(_artistsUrl, requestHeaders: headers);
final artists = _extractData(response)
.map((value) => new Artist.fromJson(value))
.toList();
return artists;
}
当我在 Chrome 检查器上检查网络数据时,我看到返回的 JSON 数据。我收到了错误:
html_dart2js.dart:3352 EXCEPTION: NoSuchMethodError: method not found: 'get$body' (J.getInterceptor$x(...).get$body is not a function)
代码可能不适合 getArtists 结构。
【问题讨论】: