【问题标题】:Send data via POST from IONIC 3 to php通过 POST 从 IONIC 3 向 php 发送数据
【发布时间】:2018-02-21 11:31:57
【问题描述】:

我正在尝试使用 http 库通过 post 将数据从 ionic 3 发送到 php,但是当 php 尝试检索它时,它找不到它。 这是创建要发送到 php 的令牌的 Typescript 文件,并在 localhost (xampp) 上调用 php 文件

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { Stripe } from '@ionic-native/stripe';
import { Http, Headers } from '@angular/http';

@IonicPage()
@Component({
    selector: 'page-pay',
    templateUrl: 'pay.html',
})
export class PayPage {

    cardinfo: any = {
        number: '4242424242424242',
        expMonth: '11',
        expYear: '20',
        cvc: '110'
    }

    constructor(public navCtrl: NavController, public navParams: NavParams, public stripe: Stripe, public http: Http,
        public toastCtrl  : ToastController) {
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad PagoPage');
    }

    pay(amount){
        this.stripe.setPublishableKey('[mytoken]');
        this.stripe.createCardToken(this.cardinfo).then((token) => {
            var headers = new Headers();
            headers.append('Content-Type', 'application/json');
            var body = {
                stripetoken: token
            };
            var myData = JSON.stringify({stripetoken: token});
            var url = 'http://192.168.1.2/service/pago.php';
            this.http.post(url, body, {headers: headers})
            .subscribe( (data) =>{
                if(data){
                    console.log(data);
                }
            });
        })
    }
}

这是我试图获取令牌的 php 文件,但它无法通过第一个 if,说 post 上没有任何内容

<?php 
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
require_once('./Stripe/init.php');


if ($_POST) {
    \Stripe\Stripe::setApiKey("sk_test_KsGJt7QwRWkNodymdTJmnrYr");
    $error = '';
    $success = '';
    try {
        if (!isset($_POST['stripetoken'])){
            throw new Exception("The Stripe Token was not generated correctly");
        }

        \Stripe\Charge::create(array(
            "amount" => 100,
            "currency" => "usd",
            "source" => $_POST['stripetoken'],
            "description" => "Charge for daniel.wilson@example.com"
            ));
        $success = 'Your payment was successful.';
        echo json_encode($success);
    }
    catch (Exception $e) {
        $error = $e->getMessage();
        echo json_encode($error);
    }
}else{
    echo json_encode("nothing was send");
}
?>

有人知道它为什么不工作吗?或我可能尝试的任何不同方法

【问题讨论】:

    标签: php typescript ionic-framework ionic2 stripe-payments


    【解决方案1】:

    如果您还没有解决这个问题,this 可能会有所帮助。

    基本上,它不能正确读取$_POST,所以试试这个:

    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata); 
    

    这里,$request 是帖子数据,所以$_POST['stripetoken'] 返回为$request-&gt;stripetoken

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2018-09-01
      相关资源
      最近更新 更多