Welcome Guest, Not a member yet? Register   Sign In
Send data with ajax alway responds error !
#1

[eluser]misakitran[/eluser]
I use ajax jquery to update data, but always responded error.

---------- HTML ---------
Code:
<div class="Head">My bag (<span></span>)</div>

&lt;form action="http://localhost:82/shop/product/add_to_cart" method="post" accept-charset="utf-8" class="CartForm"&gt;
    <div class="Product">
        <div class="Pic"></div>
        <div class="Info">
            <a class="Buy">Buy</a>
        </div>
        &lt;input type="hidden" name="id" value="1"&gt;
    </div>
&lt;/form&gt;

---------- JS---------
$(document).ready(function() {  
    
    var link = "http://localhost:82/shop/product/"; // i am testing on local ^.^
    
    $('a.Buy').click( function() {
        form = $(this).parent().parent().parent();
        form.submit();
    });
    
    $('form.CartForm').submit( function() {
        // get id of product
        var id = $(this).find('input[name=id]').val();
        
        $.ajax({
            type: 'POST',
            url: link + 'add_to_cart',
            data: { id: id },
            success: function(respond) {
                $('div.Head > span').html(respond);
            },
            error: function() {
                $('div.Head > span').html('error'); // always respond here :(
            }
        });
        return false;        
    });
});
---------- PHP ---------
function add_to_cart()
{
   $id = $this->input->post('id');
   if ($id != FALSE) {
       echo $id;
   }
   else {
       echo "false";
   }
}

My problem is when submit, responding data always is 'error'.
Please help me ! Thanks !
#2

[eluser]borgir[/eluser]
You have different urls:
http://localhost:82/shop/product/add_to_cart
http://localhost:82/shop_memup/product/
Does the second one really exists?
#3

[eluser]misakitran[/eluser]
thanks borgir ! i see that. Actually i post sort url to easy looking.
Any reason ?
#4

[eluser]borgir[/eluser]
Try this url in your browser:
http://localhost:82/shop/product/add_to_cart
What's the output?
And this one:
http://localhost:82/shop_memup/product/add_to_cart
What's the output?
#5

[eluser]InsiteFX[/eluser]
Its best to install FireFox with FireBug to see what is going on.
#6

[eluser]misakitran[/eluser]
Thanks all ! I have done.




Theme © iAndrew 2016 - Forum software by © MyBB