2017-06-25 69 views
1

我有一個關於Ionic 2的項目,我試圖從我的組件文件中發送一個值到我的php中,所以我可以將它用於我的sql查詢。我有我的價值作爲navParams,但我不知道如何將其發送到PHP,既不知道如何接收它。如何發送數據並從組件獲取數據到php? Ionic 2

我已經嘗試了很多方法,但到目前爲止,我得到了JSON位置意外標記0

組件文件:

import { Infoproducto } from './../infoproducto/infoproducto'; 
    import { ServiceProvider } from './../../providers/service/service'; 
    import {BarcodeScanner , BarcodeScannerOptions} from '@ionic-native/barcode-scanner'; 
    import { Component } from '@angular/core'; 
    import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular'; 

    /** 
    * Generated class for the Home2Page page. 
    * 
    * See http://ionicframework.com/docs/components/#navigation for more info 
    * on Ionic pages and navigation. 
    */ 
    @IonicPage() 
    @Component({ 
     selector: 'page-home2', 
     templateUrl: 'home2.html', 
    }) 
    export class Home2Page { 

    options: BarcodeScannerOptions; 
     results:{}; 
     products2 : any[]; 
     cate : any[]; 
     id : any; 



    constructor(private barcode : BarcodeScanner , public navParams: NavParams,public navCtrl: NavController, public service : ServiceProvider, public alertCtrl: AlertController) { 
       this.id = navParams.get('gaming'); 
     } 

     ionViewWillEnter(){ 
     this.getListarHome2(); 
     this.getListarCate(); 
     } 

     getListarHome2(){ 
      this.service.ListarProdCate().subscribe(
       data => this.products2 = data, 
       err => console.log(err) 
      );  
      } 

     getListarCate(){ 
      this.service.ListarCate().subscribe(
       data2 => this.cate = data2, 
       err => console.log(err) 
      );  
      } 

     moverCategoria(gaming) 
     { 
     //this.id = { id : gaming.text}; 
     console.log(gaming); 
     this.navCtrl.push(Home2Page,{"gaming": gaming}); 
     } 
     async scanBarcode(){ 

     this.options = { 
      prompt : 'Scanner barcode to see the result!.' 
     } 
     this.results = await this.barcode.scan(this.options); 
     console.log(this.results); 
     } 

     loadinfoprod(){ 

      this.navCtrl.push(Infoproducto); 

     } 
    } 

PHP文件:

<?php 

    header("Access-Control-Allow-Origin:http://localhost:8100"); 
    header("Content-Type: application/x-www-form-urlencoded"); 
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

     $data = file_get_contents('php://input'); 
     $objData = json_decode($data); 

     //$objData->gaming; 


     $gaming = $objData->gaming; 

    $datos; 
    $resultados_finalees; 

    @$db = mysqli_connect("localhost","root","","speedomart"); 

    if($db) 
    { 
      $query = "SELECT producto.codigo_barra, producto.nombre, producto.imagen, producto.precio FROM producto WHERE producto.idCategoria = 13"; 
      //$query = "SELECT producto.codigo_barra, producto.nombre, producto.imagen, producto.precio, stock_prod.ubicacion from producto JOIN stock_prod ON stock_prod.idSup = 1 ORDER BY RAND() LIMIT 0,2"; 
      $data=mysqli_query($db,$query); 

      while($fila=mysqli_fetch_array($data)) 
      { 
       $codigo = $fila[0]; 
       $nombre = $fila[1]; 
       $imagen = $fila[2]; 
       $precio = $fila[3];    


       $resultados_finalees[] = array("mensage"=>"algcorrecto","codigo"=>$codigo,"nombre"=>$nombre,"imagen"=>$imagen,"precio"=>$precio); 

      } 

       echo json_encode($resultados_finalees); 


    }else 
    { 
     $resultados_finalees = array("mensage"=>"credenciales incorrectas"); 
     echo json_encode($resultados_finalees); 
    }; 

    ?> 

回答

1

使用角的HTTP服務向後端發送Ajax請求(例如,PHP,python,node.js ...)。然後,您可以從http GET params或http POST主體獲取請求數據。

這將是這樣的:

import 'rxjs/add/operator/toPromise'; 
import { Http } from '@angular/http'; 

... 

getHero(id: number): Promise<Hero> { 
    const url = `${this.heroesUrl}/${id}`; 
    return this.http.get(url) 
    .toPromise() 
    .then(response => response.json().data as Hero) 
} 

也許這個教程可以幫助您:doc

+0

該代碼在哪裏去,我該如何適應它? –

+0

您可以在組件中添加註入Http服務,但更優雅的方式是在您自己的服務中處理http請求。 – awmleer

+0

你最好看看教程... https://angular.io/tutorial – awmleer

相關問題