如何在json变量中存储输出并成功绘制它?

时间:2022-04-19 14:04:57

I am using Opencart 2.0.2.0. Before that i was using Opencart 1.5.6.4. My code is as below,

我正在使用Opencart 2.0.2.0。在此之前我使用的是Opencart 1.5.6.4。我的代码如下,

       if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/onepagecheckout/address.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/onepagecheckout/address.tpl';
        } else {
            $this->template = 'default/template/onepagecheckout/address.tpl';
        }

        $json['hasshipping'] = $this->cart->hasShipping();  
        $json['output'] = $this->render();
        $this->response->setOutput($this->model_onepagecheckout_checkout->jsonencode($json));

This was working fine. I get it on checkout page and i render HTML by json['output']. But in Opencart 2.0.2.0 i can not do the same.

这工作正常。我在结帐页面上得到它,我通过json ['output']呈现HTML。但在Opencart 2.0.2.0中我不能这样做。

My code is as below

我的代码如下

      $json['hasshipping'] = $this->cart->hasShipping();    

      $json['output'] = $this->response->setOutput($this->load->view('default/template/onepagecheckout/address.tpl', $data));

}
      $this->response->addHeader('Content-Type: application/json');
      $this->response->setOutput($this->model_onepagecheckout_checkout->jsonencode($json));

I am getting it as an HTML in console. And i have also checked dataType is JSON in my ajax code. How can i solve it. Thanks!

我在控制台中将其作为HTML获取。我还检查了我的ajax代码中的dataType是JSON。我该如何解决呢。谢谢!

Edit

Here is the snap of response of version 1.5.6.4

这是版本1.5.6.4的响应快照

如何在json变量中存储输出并成功绘制它?

Here is the response image of version 2.0.2.0

这是2.0.2.0版的响应图像

如何在json变量中存储输出并成功绘制它?

1 个解决方案

#1


just need to remove $this->response->setOutput when load view to json result.
so the code will look like this :

只需要在将视图加载到json结果时删除$ this-> response-> setOutput。所以代码看起来像这样:

$json['hasshipping'] = $this->cart->hasShipping();
$json['output'] = $this->load->view('default/template/onepagecheckout/address.tpl', $data);

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));

#1


just need to remove $this->response->setOutput when load view to json result.
so the code will look like this :

只需要在将视图加载到json结果时删除$ this-> response-> setOutput。所以代码看起来像这样:

$json['hasshipping'] = $this->cart->hasShipping();
$json['output'] = $this->load->view('default/template/onepagecheckout/address.tpl', $data);

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));