Ajax调用不仅适用于IE

时间:2011-10-10 03:11:44

标签: jquery ajax internet-explorer codeigniter shopping-cart

我已经从这个codeigniter cart and jquery from nettuts+

实现了cart / jquery ajax教程

除了IE之外,它在所有浏览器中都很棒。我认为它与css选择器有关,可能早期版本的IE不支持。它没有做的是按照它想象的那样加入购物车。我知道帖子是成功的,但是返回到div的数据的.get和加载不是。我知道帖子正在运行,因为如果您点击更新购物车按钮,它将显示ajax应添加的所有添加项目并更新列表而不重新加载页面。

这是我的JS

    $(document).ready(function() { 
    /*place jQuery actions here*/ 
    var link = "";


    $("ul.products form").submit(function() {
        // Get the product ID and the quantity 
        var id = $(this).find('input[name=product_id]').val();
        var qty = $(this).find('input[name=quantity]').val();

         $.post(link + "cart/add_cart_item", { product_id: id, quantity: qty, ajax: '1' },
            function(data){

            if(data == 'true'){

                $.get(link + "cart/show_cart", function(cart){
                    $("#cart_content").html(cart);
                });

            }else{
                alert("Product does not exist");
            }   

         }); 

        return false;
    });

    $(".empty").live("click", function(){
        $.get(link + "cart/empty_cart", function(){
            $.get(link + "cart/show_cart", function(cart){
                $("#cart_content").html(cart);
            });
        });

        return false;
    });




});

这是ajax调用::

的处理php
<?php

class Cart extends MX_Controller { // Our Cart class extends the  hmvc (MX)Controller class

 function __construct()
    {
        parent::__construct(); // We define the the Controller class is the parent. 
        $this->load->model('cart_model'); // Load our cart model for our entire class
    }

    function index()
    {
        $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products

        $data['content'] = 'cart/cart/products'; // Select view to display
        $this->load->view('index', $data); // Display the page
    }

    function add_cart_item(){

        if($this->cart_model->validate_add_cart_item() == TRUE){

            // Check if user has javascript enabled
            if($this->input->post('ajax') != '1'){
                redirect('cart'); // If javascript is not enabled, reload the page with new data
            }else{
                echo 'true'; // If javascript is enabled, return true, so the cart gets updated
            }
        }

    }

    function update_cart(){
        $this->cart_model->validate_update_cart();
        redirect('cart');
    }

    function show_cart(){
        $this->load->view('cart/cart/cart.php');
    }

    function empty_cart(){
        $this->cart->destroy();
        redirect('cart');
    }
    function checkout(){
    $numitems=$this->cart->total_items();

    if($numitems>1){
    //more then 1 item
    $this->load->library('Paypal_Lib');
    $multiproductarray=$this->cart->contents();
    //echo var_dump($multiproductarray); return;
    $this->paypal_lib->add_field( 'business', $this->config->item( 'paypal_email' ));
            $this->paypal_lib->add_field( 'return', site_url( 'paypal/success' ) );
            $this->paypal_lib->add_field( 'cancel_return', site_url( 'paypal/cancel' ) );
            $this->paypal_lib->add_field( 'notify_url', site_url( 'paypal/ipn' ) ); // <-- IPN url
            $this->paypal_lib->multi_items('true');
            $i=1; // keeps track for _number
            foreach($this->cart->contents() as $items){
            $this->paypal_lib->add_field( 'item_name_'.$i, $items['name'] );
        $this->paypal_lib->add_field( 'item_number_'.$i, $items['id'] );
            $this->paypal_lib->add_field( 'amount_'.$i, $items['price'] );
            $this->paypal_lib->add_field( 'quantity_'.$i, $items['qty'] );
        //  $this->paypal_lib->add_field( 'quantity_'.$i, '10' );
            $i++;
            }
            redirect( $this->paypal_lib->paypal_get_request_link() );//this sends to paypal


    }else{
    //1 item
        $this->load->library( 'Paypal_Lib' );
        //$singleproductarray=$this->cart->contents();
        //echo var_dump($singleproductarray); return;
        //echo $singleproductarray['name'].$singleproductarray['id'].$singleproductarray['price']; return;
            $this->paypal_lib->add_field( 'business', $this->config->item( 'paypal_email' ));
            $this->paypal_lib->add_field( 'return', site_url( 'paypal/success' ) );
            $this->paypal_lib->add_field( 'cancel_return', site_url( 'paypal/cancel' ) );
            $this->paypal_lib->add_field( 'notify_url', site_url( 'paypal/ipn' ) ); // <-- IPN url
            $this->paypal_lib->multi_items('false');
            //$this->paypal_lib->add_field( 'item_name', $singleproductarray['name'] );
        //  /$this->paypal_lib->add_field( 'item_number', $singleproductarray['id'] );
            //$this->paypal_lib->add_field( 'amount', $singleproductarray['price'] );
                foreach($this->cart->contents() as $items){
                    $this->paypal_lib->add_field( 'item_name', $items['name'] );
        $this->paypal_lib->add_field( 'item_number', $items['id'] );
            $this->paypal_lib->add_field( 'amount', $items['price'] );
                    $this->paypal_lib->add_field( 'quantity', $items['qty'] );

            }
            redirect( $this->paypal_lib->paypal_get_request_link() );//this sends to paypal

    }

    }
}

/* End of file cart.php */
/* Location: ./application/controllers/cart.php */




<?php 

class Cart_model extends CI_Model {

    // Function to retrieve an array with all product information
    function retrieve_products(){
        $query = $this->db->get('products');
        return $query->result_array();
    }

    // Updated the shopping cart
    function validate_update_cart(){

        // Get the total number of items in cart
        $total = $this->cart->total_items();

        // Retrieve the posted information
        $item = $this->input->post('rowid');
        $qty = $this->input->post('qty');

        // Cycle true all items and update them
        for($i=0;$i < $total;$i++)
        {
            // Create an array with the products rowid's and quantities. 
            $data = array(
               'rowid' => $item[$i],
               'qty'   => $qty[$i]
            );

            // Update the cart with the new information
            $this->cart->update($data);
        }

    }

    // Add an item to the cart
    function validate_add_cart_item(){

        $id = $this->input->post('product_id'); // Assign posted product_id to $id
        $cty = $this->input->post('quantity'); // Assign posted quantity to $cty

        $this->db->where('id', $id); // Select where id matches the posted id
        $query = $this->db->get('products', 1); // Select the products where a match is found and limit the query by 1

        // Check if a row has been found
        if($query->num_rows > 0){

            foreach ($query->result() as $row)
            {
                $data = array(
                    'id'      => $id,
                    'qty'     => $cty,
                    'price'   => $row->price,
                    'name'    => $row->name
                );

                $this->cart->insert($data); 

                return TRUE;
            }

        // Nothing found! Return FALSE! 
        }else{
            return FALSE;
        }
    }

    // Needed?
    //function cart_content(){
    //  return $this->cart->total();
    //}

}


/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */

发生的事情是在第一个项目加载到购物车后(通过ajax,并且成功)它会继续添加项目但是ajax不会更新屏幕上的ul列表项目,除非我手动按下更新购物车按钮。问题仅存在于IE

中 谢谢你!

1 个答案:

答案 0 :(得分:1)

你从服务器回复什么

echo true ;,那么你应该使用if(data == true)

echo 'true' ;,那么你应该使用if(data == 'true')

echo 1;然后你应该使用if($.trim(data) == 1)

尝试使用$.trim(data)代替data

希望这可能会有所帮助。