如何通过JSON检查数据发送到PHP页面?

时间:2017-09-25 10:58:37

标签: php json ajax

当我点击php page中的JSON时,我需要检查add to basket button发送的目标OpenCart 3中的数据。

如何使用print_r在php页面中进行此操作检查? 或者有更好的方法来检查吗?

这是一个普通的JSON代码:

$.ajax({
    url: 'index.php?route=checkout/cart/add',
    type: 'post',
    data: $('#product' + id + ' input[type=\'hidden\'], #product' + id + ' input[type=\'checkbox\']:checked'),
    dataType: 'json',
    ...

PHP代码:

public function add() {
    $json = array();

    if (isset($this->request->post['product_id'])) {
        $product_id = (int)$this->request->post['product_id'];
    } else {
        $product_id = 0;
    }

    $product_info = $this->model_catalog_product->getProduct($product_id);

    if ($product_info) {
        if (isset($this->request->post['quantity'])) {
            $quantity = (int)$this->request->post['quantity'];
        } else {
            $quantity = 1;
        }

        if (isset($this->request->post['option'])) {
            $option = array_filter($this->request->post['option']);
        } else {
            $option = array();
        }

        $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);

        foreach ($product_options as $product_option) {
            if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
                $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
            }
        }

        if (!$json) {
            $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);

            $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));

            // ...

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

很容易找到json中发布到php文件的内容。只需使用下面的行

$json = file_get_contents('php://input');

然后解码json

$dec = json_decode($json, true);

print_r($dec); // this prints the array