由于转换函数,AJAX JSON解析错误?

时间:2016-01-05 17:52:20

标签: jquery json ajax opencart

我有一个函数,在单击按钮后,遍历每个选定元素,通过$ .ajax检索数据,然后将json数据写入新窗口。

当我只选择一个元素时,它的效果非常好。但是,如果我选择多个,我会在JSON数据的第1行第1列收到" SyntaxError:JSON.parse:意外字符"。

编辑:: 我在.tpl文件中找到了一个函数,当省略时,页面加载时没有解析错误。我使用此函数将字符串转换为货币。如何重写此功能以便我仍然可以使用它?它在像array_walk这样的地方调用($ product_total," convertCurrencyStringtoNumber");

function convertCurrencyStringtoNumber(&$item, $key) {
    $currency = 'USD';
    $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    $item = $formatter->parseCurrency($item, $currency);
}

AJAX功能:

$('#print_depositreport').on('click', function() {
    var order_ids = [];
    $('.selected').each(function() {
        if ($(this).attr('checked')) {
            order_ids.push($(this).attr('title'));
        }
    });
    var new_window = window.open();
    $.ajax({
        url: 'index.php?route=sale/order_entry/depositreport&token=<?php echo $token; ?>',
        type: 'GET',
        dataType: 'json',
        data: 'order_id=' + order_ids + '&type=print',
        success: function(json) {
            new_window.document.write(json);
            new_window.document.close();
            new_window.focus();
            new_window.document.write(htmlPage);
            new_window.close();
        },
        error: function(xhr,j,i) {
            alert(i);
        }
    });
});

PHP函数:

 public function depositreport() {      
    $this->load->model('sale/order_entry');
    $this->load->model('setting/setting');
    $this->load->model('sale/order');

    $orders = array();
    $html = "";
    $print_multi_html = "";

    if (isset($this->request->post['selected'])) {
    $orders = $this->request->post['selected'];
    } elseif (isset($this->request->get['order_id'])) {
        $orders = explode(",", $this->request->get['order_id']);
    } elseif (isset($this->session->data['edit_order'])) {
        $orders[] = $this->session->data['edit_order'];
    }
    foreach ($orders as $order_id) {
        $this->log->write('Order ID = ' . $order_id);
    }

    foreach ($orders as $order_id) {

        $order_id = $this->request->get['order_id'];
        $order_info = $this->model_sale_order->getOrder($order_id);

        $language = new Language($order_info['language_directory']);
        $language->load($order_info['language_filename']);
        $language->load('sale/order');
        $language->load('sale/order_entry');

        $template = new Template();
        $template->data['orders'] = array();
        $template->data['title'] = $language->get('packing_slip_title');

        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
            $template->data['base'] = HTTPS_SERVER;
        } else {
            $template->data['base'] = HTTP_SERVER;
        }

        $template->data['direction'] = $language->get('direction');
        $template->data['language'] = $language->get('code');

        $template->data['text_deposit_report'] = $language->get('text_deposit_report');
        $template->data['text_report_date'] = $language->get('text_report_date');
        $template->data['text_store_name'] = $language->get('text_store_name');

        $template->data['column_total'] = $language->get('column_total');
        $template->data['column_order_id'] = $language->get('column_order_id');
        $template->data['column_customer'] = $language->get('column_customer');
        $template->data['column_check_number'] = $language->get('column_check_number');
        $template->data['column_source_code'] = $language->get('column_source_code');
        $template->data['column_subtotal'] = $language->get('column_subtotal');

        $this->load->model('setting/setting');

        $store_info = $this->model_setting_setting->getSetting('config', $order_info['store_id']);

        if ($order_info) {
            $admin_folder = basename(HTTP_SERVER);
            $store_url = str_replace($admin_folder, "", $order_info['store_url']);

            $product_data = array();
            $products = $this->model_sale_order->getOrderProducts($order_id);

            foreach ($products as $product) {

                if ($this->config->get('config_product_price_tax')) {
                    if (isset($this->session->data['store_id'])) {
                        $price = $this->currency->format($product['price'] + ($this->session->data['store_config']['config_tax'] ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']);
                        $total = $this->currency->format($product['total'] + ($this->session->data['store_config']['config_tax'] ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']);
                    } else {
                        $price = $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']);
                        $total = $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']);
                    }
                } else {
                    $price = $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value']);
                    $total = $this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value']);
                }
                $product_data[] = array(
                    'source_code'  => $product['source_code'],
                    'total'    => $total
                );
            }

            $total_data = array();
            $total_data = $this->model_sale_order->getOrderTotals($order_id);

            $template->data['orders'][] = array(
                'order_id'              => $order_id,
                'store_name'            => $order_info['store_name'],
                'product'               => $product_data,
                'check_number'       => $order_info['check_number'],
                'firstname'          => $order_info['payment_firstname'],
                'lastname'           => $order_info['payment_lastname'],
                'total'              => $total_data
            );
            $html = $template->fetch('sale/order_entry_depositreport.tpl');
            $print_multi_html .= $html;
        }
    }
    $this->response->setOutput(json_encode($print_multi_html));
}

0 个答案:

没有答案