【问题标题】:Ionic 4 POST to PHP backendIonic 4 POST 到 PHP 后端
【发布时间】:2019-04-06 09:14:55
【问题描述】:

我正在尝试将数据发布到 PHP 后端并接收返回值并将其推送到数组中。因此,我创建了一个函数来做到这一点。但是,我不会更改后端的 API(用 PHP 编写)。所以我不能改变它以适应我使用 POST 的正常方法。

这是我的功能

test() {
    let data = "method=getThis" + "&db=myDatabase"
    this.http.post("API URL", data).subscribe(data => {
      this.result = data;   // get data in result variable
      this.items = JSON.stringify(this.result); // then convert data to json string
      // console.log(this.items);
      this.allData = JSON.parse(this.items); // parse json data and pass json string
      // console.log(this.allData.length); // got result of particular string
      this.array = [];
      for (var i = 0; i < this.allData.length; i++) {
        this.array.push({
          data1: this.allData[i].data1,
          data2: this.allData[i].data2,
        })
      }
      console.log(this.array[0])
    })
  }

这是后端的示例函数

else if($_POST['method']=="getThis"){
    global $conn;
    mysqli_select_db($conn, $_POST['db']);
    $name="";
    $result=array();
    $r=mysqli_query($conn,"select data1,data2 from table");
    while ($rs = mysqli_fetch_array($r,MYSQLI_ASSOC)){
        array_push($result,$rs);
    }
    echo json_encode(array("result"=>$result));
}

那么我如何才能真正发布呢?我被困在这里。我通常使用 JSON 发布,然后在后端解码 JSON。但是这一次我没有开发后端,也没有改变它,所以必须使用提供的后端。

使用 POSTMAN 发帖

method=getThis&db=myDatabase

效果很好。不发送 JSON 只是一个文本。那么我如何在 Ionic 中真正实现这一点。

【问题讨论】:

  • this.allData = "method=getThis&db=myDatabase" 可能是一种快速但懒惰的方法。
  • @JohnV。哈哈哈..不幸的是它没有用..
  • 哦,我误会了。看起来结果将是一个带有“结果”键的对象,这将是一个数组。你应该试试 this.allData = data.result

标签: php android angular typescript ionic-framework


【解决方案1】:

你可以这样试试。它对我有用:

第一次导入:

import { map } from "rxjs/operators/map";

你的功能:

test() {
    let data = "method=getThis" + "&db=myDatabase"
    this.http.post("API URL", data).pipe(
      map(res => res.json())
    ).subscribe(response => {
      //Here your code
      // 'response' is json
    });
  }

【讨论】:

  • res.json() 在 ionic 3 和 4 中不起作用.. (据我尝试).. 如果我没记错的话,因为它默认返回一个 json,所以不需要使用 res。 json 再次.. 无论如何,你运行的是什么版本的 ionic??
  • 我使用的是 4.1.1,它对我有用。这里有一段代码在 4.1.1 中完美运行: ``` this.http.post("localhost:8080/sql/get", data).pipe( map(res => res.json()) ).subscribe(response => { console.log("GET DATA FROM SQL:"); console.log(response); console.log(response[4].Name); //this.loginField = response[4].Name; // this.passwordField = response[4].Password; }); ```
【解决方案2】:

由于您发送的数据是纯文本格式,因此您需要添加一个标头来提及内容类型。 import { HttpHeaders } from '@angular/common/http';

const httpOptions = {
     headers: new HttpHeaders({
    'Content-Type':  'text/html'
    })
};

this.http.post("API URL", data, httpOptions).subscribe()

【讨论】:

  • this.http 我从那里得到这个 (http)
  • 那必须像import { HttpClient } from '@angular/common/http';那样导入到特定的文件中,然后像constructor ( public http: HttpClient )那样将它添加到构造函数中
【解决方案3】:

PHP 端应返回 JSON 并告知浏览器内容类型为 application/json,请在一个简单的页面上测试您的代码库。

//demo.php
<?php
$data = ['message' => 'Hello world.'];
header("Content-Type: application/json; charset=UTF-8");

//If allow cross domain and not configration in Ngix/Apache
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");

echo json_encode($data);

请再次尝试 http 访问 demo.php。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2020-02-26
    • 2019-01-01
    相关资源
    最近更新 更多