我正在使用Windows上的Nativecript Sidekick Cloud byar在iOS上测试我的应用程序 - 我还连接了我的iPhone。
根据这篇文章,我将我的HTTP请求发送给Localhost,但他们一直在此错误中失败。
http http://localhost:52553/api/rewards/all:0未知错误
错误:无法连接到服务器。
这是我对奖励服务的实施
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable, of } from 'rxjs';
import { SimpleReward } from "../models/simple-reward";
import { take, catchError } from "rxjs/operators";
@Injectable()
export class RewardsService {
private baseUrl = "http://localhost:52553/api/rewards";
constructor(private http: HttpClient) { }
getAllRewards(): Observable<SimpleReward[]> {
const url = `${this.baseUrl}/all`;
return this.http.get<SimpleReward[]>(url).pipe(
catchError((e) => {
console.error(e);
return of([]);
})
);
}
}
请指导我在这里做错了什么?
添加localhost
作为app/App_Resources/iOS/Info.plist
文件的例外,以允许使用http://localhost
域的Insecure(non-HTTPS(通信:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
这比允许与 all 域的不安全通信更可取。
编辑:另外,请记住,您必须在编辑该Info.plist
文件后重建应用程序!它的更改不能像JS一样简单地实时同步或热模块重新加载。
您可以禁用安全性或将localhost添加为info.plist中的异常。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>