首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从组件中发送和获取数据到php?离子2

如何从组件中发送和获取数据到php?离子2
EN

Stack Overflow用户
提问于 2017-06-25 08:06:54
回答 1查看 627关注 0票数 2

我在Ionic 2上有一个项目,我试图将组件文件中的一个值发送到我的php中,这样我就可以在sql查询中使用它。我的值是navParams,但我不知道如何将它发送到php,也不知道如何接收它。

我尝试了很多方法,但到目前为止,我在JSON位置0得到了意想不到的标记

组件文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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);
    };

    ?>
EN

回答 1

Stack Overflow用户

发布于 2017-06-25 08:18:03

使用Angular的HTTP服务向后端发送Ajax请求。PHP,python,node.js...)。然后您可以从http GET params或http POST body获取请求数据。

它应该是这样的:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44742008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档