簡體   English   中英

Ionic2創建PHP以獲取要在頁面中顯示的數據

[英]Ionic2 create PHP to get data for show in page

我想知道如何創建PHP以獲取show home.html的數據

我得到`錯誤數據未定義

我不知道這是正確的嗎? 請檢查一下。 我不打算創建PHP,我想設置$ path來保留我的URL。

<ion-content>
    <ion-list inset>
        <ion-item>
            <ion-label>Username</ion-label>
            <ion-input [(ngModel)]="data.username" type="text"></ion-input>
        </ion-item>
    </ion-list>
    <div padding>
        <button ion-button block (click)="getRepos()">Search</button>
    </div>
    <ion-card *ngFor="let repo of foundRepos" >
        <ion-card-header>
            {{ repo.data.name }}
        </ion-card-header>
        <ion-card-content>
            {{ repo.data.description }}
        </ion-card-content>
    </ion-card>
</ion-content>

import { Component } from "@angular/core";
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {
  public foundRepos;

  constructor(public http: Http) {
        this.data = {};   >>>>>>>data is not defined
        data.username = ''; >>>>>data is not defined
        this.http = http;
    }

   getRepos() {
        var link = 'http://localhost/github.php';
        var data = JSON.stringify({username: this.data.username}); <<<<this.data.username (data is not defined)

        this.http.get(link, data) <<<<data is not defined
        .subscribe(data => {
         this.foundRepos = data.json();
       },
        err => console.error(err),
        () => console.log('getRepos completed')
    );
  }

}

github.php

<?php


    $postdata = file_get_contents("php://input");
    if (isset($postdata)) {
        $request = json_decode($postdata);
        $username = $request->username;

        if ($username != "") {
            $path = "https://api.github.com/users/".$username."/repos";
            echo $path ;
        }
        else {
            echo "Empty username parameter!";
        }
    }
    else {
        echo "Not called properly with username parameter!";
    }
?>

啊,我明白了。

在您的php中,您正在檢索一個名為$postdata的值。 因此,我假設您要將用戶名從ionic 2應用程序發送到您的php文件。

2個選項,您應該同時嘗試這兩個選項,因為我不了解php,因此不確定是否正確的解決方案。

但我確實知道問題出在哪里。 您正在進行http.get()調用,並在其中傳遞了2個參數。 linkdata GET方法用於從鏈接獲取數據,而不是提供數據。 http.get有兩個參數, url (在您的情況下為link )和HttpHeaders對象,而不是 data。

在http.get中帶有參數的解決方案

因此,如上所述, http.get()無法發送您的data 除非您將其添加為URL中的參數。 然后,您的打字稿將如下所示(請注意:反引號而不是'',使字符串的合並更容易):

var link = `http://localhost/github.php?username=${this.data.username}`;
this.http.get(link).subscribe(data) { .... }

並在php中替換

$username = $response->username

$username = $_GET['username']

所以最終的打字稿看起來像這樣(因為php get請求返回了403,Github可能不允許這樣做)

getRepos() { 
  var link = `localhost/…${this.data.username}`; 

  this.http.get(link) 
    .subscribe(data => { 
      let repoUrl = data.text(); 

      this.http.get(repoUrl).subscribe(githubResponse => { 
        this.foundRepos = githubResponse.json(); 
      }); 
  }, 
  err => console.error(err), 
  () => console.log('getRepos completed') 
  ); 
}

如果有人想詳細解釋為什么我們得出這個答案,請在下面的聊天窗口中瀏覽

您尚未在該類中聲明數據變量。

export class HomePage {
  public foundRepos;
  public data;//here
  constructor(public http: Http){
    this.data = {};
    this.data.username = '';//to refer to data in any function always use 'this'
  }
//other functions

暫無
暫無

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

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