簡體   English   中英

不允許到本地主機的 NativeScript Angular 明文 HTTP 流量

[英]NativeScript Angular Cleartext HTTP traffic to localhost not permitted

我正在使用原生腳本的 angular 版本並嘗試為我的網絡創建登錄系統。
下面是我嘗試登錄網絡的代碼。

home.component.ts

 import { LoginService } from '../Services/login/login.service';
 export class LoginComponent implements OnInit {

 public user: User;
 loginForm: FormGroup;

 constructor(private router: Router,
     private userService: UserService,
     private page: Page,
     public  loginDetails: LoginDetails,
     private LoginService: LoginService

 ) {

     this.user = new User();
     this.user.email = "m@m.com";
     this.user.password = "pass123";
     this.user.logo = "~/Images/opos_logo.png";
 }

 submit() {

    /* set a user variable with data from the formGroup */
    const user: string = this.user.email;
    /* set a password variable with data from the formGroup */
    const password: string = this.user.password;

    this.LoginService.loginUser(user, password)
        .subscribe(data => this.checkUser(data));
 }
}

服務/登錄/登錄.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from "@angular/core";
import { Jwt } from '../../Classes/jwt';


@Injectable({
    providedIn: 'root'
})
export class LoginService {


  loginUrl = 'http://localhost/Login.w';

  constructor(private _http: HttpClient) { }

  loginUser(user, pass) {
      return this._http.get<Jwt>(
          this.loginUrl, { params: { fi_user: user, fi_pass: pass } });
  }
}

當我嘗試像這樣運行我的應用程序時,我會收到以下錯誤:

“originalStack”:“錯誤:java.io.IOException:在新的 ZoneAwareError (file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/ angular/zone-js/dist/zone-nativescript.js:1298:31)\\n 在 onRequestComplete (file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/core/http /http-request/http-request.js:54:30)\\n 在 Object.onComplete (file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/core/http/ http-request/http-request.js:43:7)"

嘗試按照Android 8: Cleartext HTTP traffic not allowed 一文中的建議解決問題,並遵循該帖子中的建議。 我在我的程序中添加了一個 androidmanifest.xml 文件(這不是應用程序的一部分,因此我不確定應用程序是否正在訪問該文件)在路徑 /App_Resources/Android/AndroidManifest.xml 上。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">

<supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:xlargeScreens="true"/>

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="__APILEVEL__"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:name="com.tns.NativeScriptApplication"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:networkSecurityConfig="@xml/network_security_config">

    <activity
        android:name="com.tns.NativeScriptActivity"
        android:label="@string/title_activity_kimera"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@style/LaunchScreenTheme">

        <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.tns.ErrorReportActivity"/>
</application>

我還添加了一個 network_security_config.xml 文件,堆棧溢出帖子建議路徑 res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">domain.com (to be adjusted)</domain>
   </domain-config>
</network-security-config>

我還嘗試將應用程序 android:usesCleartextTraffic="true" 標記添加到我的 home.component.html 以修復錯誤,但這並沒有解決問題。

嘗試:

loginUrl = 'http://10.0.2.2/Login.w';

我剛剛讀過文章: https: //stackoverflow.com/a/48591525 @EdwardGarson 寫道:

在模擬器中,localhost 或 127.0.0.1 指的是模擬器本身,而不是您的本地機器。 您需要使用 ip 10.0.2.2,它被橋接到您的本地機器。

你好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM