iOS上的Nativescript本地主机http调用失败 [英] Nativescript localhost http call fails on iOS
问题描述
我正在Windows上使用Nativescript sidekick云构建来在iOS上测试我的应用-我还连接了iPhone.
I am using Nativescript sidekick cloud build on windows to test my app on iOS - I have also connected my iPhone.
根据这篇帖子,我正在将我的http请求发送到本地主机,但它们始终因此错误而失败.
According to this post I am sending my http requests to localhost but they keep failing with this error.
http://localhost:52553/api/rewards/all 的Http失败响应:0未知错误
错误:无法连接到服务器.
Http failure response for http://localhost:52553/api/rewards/all: 0 Unknown Error
Error: Could not connect to the server.
这是我实施的奖励服务
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([]);
})
);
}
}
请在这里指导我做错了什么?
Please guide what I am doing wrong here?
推荐答案
将localhost
作为例外添加到app/App_Resources/iOS/Info.plist
文件中,以允许与http://localhost
域的不安全(非HTTPS)通信:
Add localhost
as an exception to your app/App_Resources/iOS/Info.plist
file to allow insecure (non-HTTPS) communication with the http://localhost
domain:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
这比允许与所有域的不安全通信更为可取.
This is preferable to allowing insecure communication with all domains.
编辑:此外,请记住,在编辑该Info.plist
文件后必须重新构建应用!它的更改不能像JS那样简单地实时同步或重新加载热模块.
Also, remember that you must rebuild your app after editing that Info.plist
file! Its changes cannot simply be live-synced or hot module reloaded like JS.
这篇关于iOS上的Nativescript本地主机http调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!