带有php + mysql的Ionic 2无法显示Ionic中的JSON数据

时间:2018-08-14 02:50:21

标签: php json ionic-framework mysqli

我试图通过PHP从MySQL数据库中获取数据,并在Ionic 2列表上显示信息。在控制台中,它返回未定义。

我已经尽力不显示任何内容。每次我运行该应用程序到列表页面时,它仍然不会显示数据。

有人可以告诉我我在做什么错了。

.ts

 getPosts() {
        let loader = this.loadingCtrl.create({
          content: "Loading Incidents..."
        });
        loader.present();
        this.postService.getPosts().subscribe((val) => {
          this.posts = val.posts;
          loader.dismiss();
          console.log(this.posts);
        }); 
      }

.html

<ion-card *ngFor="let item of posts" class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
        <img [src]="item.image" alt="">
        <ion-card-content>
          <ion-card-title>
            {{item.event}} -  {{item.location}}
          </ion-card-title>
          <p>
            {{item.description}}
          </p>
        </ion-card-content>

      </ion-card>

在我的php文件中,我有这个

<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {

        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");

        header('Access-Control-Allow-Credentials: true');

        header('Access-Control-Max-Age: 86400');    // cache for 1 day

    }

    // Access-Control headers are received during OPTIONS requests

    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))

            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");        

       if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))

            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);

    }


require "config.php";

  $data = file_get_contents("php://input");
    if (isset($data)) {

        $request = json_decode($data);

    }


$result = mysqli_query($db,"SELECT * FROM image_upload ORDER BY id DESC") or die(mysqli_error($db));

//create an array
    $emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }

echo json_encode($emparray);
mysqli_close($db);

 ?>

谢谢。

0 个答案:

没有答案