[英]CodeIgniter Return HTML in JSON Response
我正在尝试在codeigniter中构建一个支持ajax的购物车,现在我遇到了如何将响应作为HTML进行编码并将其编码为JSON响应的问题,然后在购物车页面附加响应。
这是javascript代码:
$('.addtocart').on('click', function (event) {
event.preventDefault();
var AddedQty = parseInt($('#attr-quantity').val());
$('#shoppingcart div.cart, #shoppingcart div.loading').remove();
$('#shoppingcart').append('<div class="loading"></div>');
shoppingcart.open();
$.post('/mywebsite/cart/add', {
itemId: $('.addtocart').data('itemId'),
quantity: AddedQty
}, function (response) {
var html = $(response.ShoppingCartHtml).html();
shoppingcart.update(html);
shoppingcart.close();
});
});
这是购物车控制器的代码:
public function add() {
$this->load->model('cart_model');
$id = $this->input->post('itemId');
$qty = $this->input->post('quantity');
$cart = $this->cart->contents();
$exists = false;
$rowid = '';
foreach ($cart as $item) {
if ($item['id'] == $id) {
$exists = true;
$rowid = $item['rowid'];
$qty = $item['qty'] + $qty;
}
}
if ($exists) {
$this->cart_model->update_item($rowid, $qty);
} else {
$this->cart_model->add_cart_item();
}
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));
}
下面的代码是我想将其编码为JSON响应的示例代码(不是真正的代码),如ShoppingCartHtml:
<li>
<h3><?php echo $ProductName; ?></h3>
</li>
到目前为止,我已经尝试回显视图,并使用json_encode对其进行编码,但是我收到了错误。 这是我带来的:
$theHTMLResponse= echo $this->load->view('pages/minicart.php', $data); //THIS LINE THROWS ERROR (I know that I cannot assign what echo-ed into a variable).
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));
我们想要的正确响应示例如下面的代码(如Firebug中所示):
{"MinicartHtml":"\u003cli\u003e\r\n\u003ch3\u003eThe Product Name\u003c/h3\u003e\u003c/li\u003e"}
如果我在Firebug控制台中检查过,在JSON选项卡上,它应该显示ShoppingCartHtml的html代码,用引号括起来,作为ShoppingCartHtml JSON Response。
问题是:如何将ShoppingCartHtml的html代码编码为JSON响应?
PS:如果我的问题令人困惑,我表示歉意。 英语不是我的一杯茶。 即使输入这个问题,我也需要将近1个小时才能完成。 但我希望你们明白我的要求。
先感谢您。
你接近正确。 只需要进行一些调整。
//set the 3rd param to true to make it return data
$theHTMLResponse = $this->load->view('path/to/view.php', null, true);
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));
我认为shoppingcart
设置为从$('#...')返回的东西。 那你只需要这个:
function (response) {
shoppingcart.html(response.ShoppingCartHtml);
shoppingcart.close();
}
首先将视图调用中的第三个参数传递为true
第三个可选参数允许您更改函数的行为,以便将数据作为字符串返回,而不是将其发送到浏览器。 如果要以某种方式处理数据,这可能很有用。 如果将参数设置为true(布尔值),它将返回数据。 默认行为为false,将其发送到您的浏览器。 如果要返回数据,请记住将其分配给变量
$theHTMLResponse= echo $this->load->view('pages/minicart.php', $data,true); //THIS LINE THROWS ERROR (I know that I cannot assign what echo-ed into a variable).
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(
array( 'sucesss'=>1, 'ShoppingCartHtml'=> $theHTMLResponse)
));
还可以使用$.getJSON
并替换$(response.ShoppingCartHtml).html();
来自response.ShoppingCartHtml
$.getJSON('/mywebsite/cart/add', {
itemId: $('.addtocart').data('itemId'),
quantity: AddedQty
}, function (response) {
if(response.success){
var html = response.ShoppingCartHtml;
shoppingcart.update(html);
shoppingcart.close();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.