当我点击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'));
// ...
非常感谢任何帮助。
答案 0 :(得分:0)
很容易找到json中发布到php文件的内容。只需使用下面的行
$json = file_get_contents('php://input');
然后解码json
$dec = json_decode($json, true);
print_r($dec); // this prints the array