user1161098
user1161098

Reputation: 11

jQuery $.ajax function for $.post

In the MvcMusicStore tutorial, it uses the $.post() to remove an item from the shoppingcart. I want to practice my jQuery and want to convert it to $.ajax() call. But it gave me a runtime error. Can anyone tell me what went wrong in my codes.

this is $.post() call

    $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
        function (data) {
             Successful requests get here
             Update the page elements
            if (data.ItemCount == 0) {
                $('#row-' + data.DeleteId).fadeOut('slow');
            } else {
                $('#item-count-' + data.DeleteId).text(data.ItemCount);
            }

            $('#cart-total').text(data.CartTotal);
            $('#update-message').text(data.Message);
            $('#cart-status').text('Cart (' + data.CartCount + ')');
        });

====================================================================================== this is my $.ajax() call.

        $.ajax({
            type: "POST",
            uri: "/ShoppingCart/RemoveFromCart",
            data: { "id": recordToDelete },
            success: function (d) {
                alert(d);
                if (d.ItemCount == 0) {
                    $('#row-' + d.DeleteId).fadeOut('slow');
                } else {
                    $('#item-count-' + d.DeleteId).text(d.ItemCount);
                }

                $('#cart-total').text(d.CartTotal);
                $('#update-message').text(d.Message);
                $('#cart-status').text('Cart (' + d.CartCount + ')');
            },
            dataType: "application/json"
        });

Upvotes: 0

Views: 185

Answers (3)

mandava
mandava

Reputation: 346

$.ajax({
        type: "POST",
        url: "ShoppingCart/RemoveFromCart",
        contenttype: "application/json; charset=utf-8",
        data: { id: recordToDelete },
        success: function (d) {
            alert(d);
            if (d.ItemCount == 0) {
                $('#row-' + d.DeleteId).fadeOut('slow');
            } else {
                $('#item-count-' + d.DeleteId).text(d.ItemCount);
            }

            $('#cart-total').text(d.CartTotal);
            $('#update-message').text(d.Message);
            $('#cart-status').text('Cart (' + d.CartCount + ')');
        }

    });

try the above code once and let me know whether your problem still persists.....

Upvotes: 0

JoseP
JoseP

Reputation: 631

the correct paramenter name is url not uri

Upvotes: 1

jogesh_pi
jogesh_pi

Reputation: 9782

use url: instead of uri

$.ajax({
        type: "POST",
        url: "/ShoppingCart/RemoveFromCart",
        data: { "id": recordToDelete },
        success: function (d) {
            alert(d);
            if (d.ItemCount == 0) {
                $('#row-' + d.DeleteId).fadeOut('slow');
            } else {
                $('#item-count-' + d.DeleteId).text(d.ItemCount);
            }

            $('#cart-total').text(d.CartTotal);
            $('#update-message').text(d.Message);
            $('#cart-status').text('Cart (' + d.CartCount + ')');
        },
        dataType: "application/json"
    });

for more information visit on $.ajax()

Upvotes: 3

Related Questions