我在Ionic 2上有一个项目,我试图将我的组件文件中的值发送到我的php中,所以我可以将它用于我的SQL查询。我有自己的价值作为navParams,但我不知道如何将它发送到PHP,也不知道如何接收它。
我尝试了很多方法,但到目前为止,我在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);
};
?>
答案 0 :(得分:1)
使用Angular的HTTP
服务向后端发送Ajax请求(例如PHP,python,node.js ......)。然后,您可以从http GET params或http POST body获取请求数据。
这将是这样的:
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。