【问题标题】:IONIC 4 & PHP when Build the apk does not workIONIC 4 & PHP 在构建 apk 时不起作用
【发布时间】:2020-02-27 11:19:38
【问题描述】:

我在 ionic 4 中创建了一个连接到 php 文件的应用程序,该文件使在 msql 中插入数据的功能一切正常,但在编译时它在 androd 9 中不起作用,不发送数据或连接到服务器。我记得很久以前我做了一些事情让它工作但我不再有源代码,我记得我放了一些“HEADER”但我不知道在哪里。

这是我的 ts 文件

'''
    import { Component, OnInit } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { AlertController } from '@ionic/angular';
    import { NavController } from '@ionic/angular';
    @Component({
      selector: 'app-factura',
      templateUrl: './factura.page.html',
      styleUrls: ['./factura.page.scss'],
    })
    export class FacturaPage implements OnInit {

      constructor(private http: HttpClient,public alertController: AlertController,public navCtrl: NavController) { }

      ngOnInit() {
      }

      datos: any = {};
    apiUrl='http://072atizapan.mx/index.php'

    logForm() { 
      let newData={
        funcion: 'correo2',
        datos: this.datos
      }

      this.http.post(this.apiUrl, JSON.stringify(newData))
      .subscribe(data=>{
        if(data=='true'){
          console.log('return');
          this.presentAlert();
        }else{
          this.presentAlert();
          this.navCtrl.navigateForward('/');

        }
      },
      (err)=>{
        this.presentAlert();
        this.navCtrl.navigateForward('/');
      }
      );
    }

这是我的 php

<?php
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS");
    header("Access-Control-Allow-Headers: Accept-Encoding, X-Requested-With, Content-Type, Origin, Accept, Authenticationtoken");

    if($_SERVER['REQUEST_METHOD'] !== 'POST'){
    echo json_encode(array('status' => false));
    exit;
       }

$postdata = file_get_contents("php://input");

$datos=json_decode($postdata, true);

switch ($datos['funcion']) {
    case 'correo2':
        getMenu($datos);
        break;
}

我已经尝试过使用白名单并在 android 上添加权限,但它对我不起作用。我确信 php 很有用,因为我已经使用过我的问题,据我说是 TS 的一部分 有什么想法吗?

ANDORID 会议

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">http://xxxxmx/</domain>
        <domain includeSubdomains="true">http://xxxx/index.php</domain>
    </domain-config>
</network-security-config>


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

解决方案:

<uses-sdk android:targetSdkVersion="27" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />



<network-security-config>
   <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
           <certificates src="system" />
       </trust-anchors>
   </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://XXX.mx/index.php</domain>
       <domain includeSubdomains="true">http://XXXX.mx</domain>
       <domain includeSubdomains="true">http://XXXX.mx/SAPASA/>
      <domain includeSubdomains="true">http://XXXX.mx/>
    </domain-config>
</network-security-config>

【问题讨论】:

  • 有同样问题的人,留下解决方案:D

标签: php mysql angular ionic-framework ionic4


【解决方案1】:

使用https:// 而不是http:// android 更新了它的安全策略,这就是为什么在最新的操作系统中不支持 http:// URL 调用。

编码愉快:-)

【讨论】:

  • 如果我使用 https 进行尝试,这在我看来“已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。”
  • 在 PHP 文件中添加以下标题:header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Methods: POST, GET, OPTIONS"); header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With"); header("Content-Type: application/json; charset=UTF-8");
【解决方案2】:

使用 Https:// 而不是 http://,它可以工作,现在最新的操作系统不支持 http:// URL,我的情况下它可以工作。

【讨论】:

  • Shailesh 的回答有何不同?
  • 如果我使用 https 进行尝试,这在我看来“已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。”
  • 更改表头顺序,将 content-type-json 放在“Access-Control-Allow-Methods”和“Access-Control-Allow-Origin: *”之后为第一行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
相关资源
最近更新 更多