我正在尝试在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响应。
问题是:如何将ShoppingCartHtml的html代码编码为JSON响应?
附言:如果我的问题令人困惑,我道歉。英语不是我喜欢的。即使要输入这个问题,我也需要近1个小时才能完成。但我希望你们能理解我的要求。
提前谢谢你。
发布于 2013-09-09 20:06:44
你差一点就做对了。只需要做几个调整。
//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)));发布于 2013-09-09 20:05:58
我认为shoppingcart被设置为从$('#...')返回的内容。那么你只需要这样:
function (response) {
shoppingcart.html(response.ShoppingCartHtml);
shoppingcart.close();
}发布于 2013-09-09 20:12:39
首先,将视图调用中的第三个参数作为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中的$(response.ShoppingCartHtml).html();
$.getJSON('/mywebsite/cart/add', {
itemId: $('.addtocart').data('itemId'),
quantity: AddedQty
}, function (response) {
if(response.success){
var html = response.ShoppingCartHtml;
shoppingcart.update(html);
shoppingcart.close();
}
});https://stackoverflow.com/questions/18697730
复制相似问题