5

I'm trying to build an ajax-powered shopping cart in codeigniter, and now I'm facing a problem with how to get the response as a HTML, and encode it as a JSON response, then append the shopping cart page with the response.

Here is the javascript code:

$('.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();
    });
});

And this is the code for the cart controller:

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)));
}

And the code below is the sample code (not the real code) that I want to encode it as the JSON response, as the ShoppingCartHtml:

<li>
    <h3><?php echo $ProductName; ?></h3>
</li>

So far, I've tried to echo the view, and encode it using the json_encode, but I get an error. Here is what I've come with:

$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)));

The example of the correct response that we wanted is like the codes below (as shown in Firebug) :

{"MinicartHtml":"\u003cli\u003e\r\n\u003ch3\u003eThe Product Name\u003c/h3\u003e\u003c/li\u003e"}

Which if I inspected in the Firebug console, on the JSON tab, it should shows the ShoppingCartHtml's html codes, enclosed with quotes, as the ShoppingCartHtml JSON Response.

The question is: How can I encode the ShoppingCartHtml's html codes as a JSON response?

PS: My apologize if my question is confusing. English is not my cup of tea. Even to type this question, I need almost 1 hour to complete it. But I hope you guys understand what I asked.

Thank you in advance.

4

3 回答 3

7

你已经接近正确了。只需要进行一些调整。

//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-09T12:06:44.457 回答
0

首先将视图调用中的第三个参数传递为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();
      }
});

意见

于 2013-09-09T12:12:38.573 回答
0

我想shoppingcart设置为从 $('#...') 返回的东西。那么你只需要这个:

function (response) { 
    shoppingcart.html(response.ShoppingCartHtml); 
    shoppingcart.close();
}
于 2013-09-09T12:05:57.853 回答