0

i write two functions in jquery client side and c# asp.net server side , i try post json list object to web method but i see error..

jquery code :

function UpdateCart(tableid) {

var page = "Account/Cart.aspx";
var method = "Update_Cart";
var url = "http://" + host + "/" + page + "/" + method;

$(".popup").show();

var cartlist = new Array();
for (var i = 68; i < 71; i++) {
    var cart = new Object();
    cart.ID = i;
    cart.Quantity = 6;
    cartlist.push(cart);
}
var jsonArray = JSON.parse(JSON.stringify(cartlist))

$.ajax({
    type: "POST",
    url: url,
    data: jsonArray,
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    async: "true",
    success: function (response) {
        // success message or do
        $(".errMsg ul").remove();

        var myObject = eval('(' + response.d + ')');

        if (myObject == 1) {

            window.location.href = "http://" + host + "/Account/cart";

        } else {

            $(".errMsg").text("نام کاربری یا رمز اشتباه است");
            $(".errMsg").removeClass("alert");
            $(".errMsg").addClass("alert alert-danger");

        }


    },
    error: function (response) {
        alert(response.status + ' ' + response.statusText);
    }
});

}

c# web method :

[WebMethod]
public static string Update_Cart(string[] Carts)
{
    if (Carts != null)
    {
        foreach (var item in Carts)
        {
            com_Shop_Carts cart = new com_Shop_Carts()
            {
                Quantity = item.Quantity,
                AddDate = DateTime.Now

            };
            com.shop.ProductManager.Update_Cart(item.ID, cart).ToString();
        }
    }

    return "1";
}

after run i see error 500 and i can't resolve it please give me solution for resolve it.

3
  • Try this data: {Carts: jsonArray}, Commented Feb 2, 2015 at 11:18
  • contact to me tehhitech [at] live.com Commented Feb 2, 2015 at 11:24
  • not work it. error 500 is Commented Feb 2, 2015 at 11:29

1 Answer 1

1

Apart from this : var jsonArray = JSON.parse(JSON.stringify(cartlist))

Try this : var json={"Carts":cartlist}; then, var jsonArray=JSON.stringify(json);

and remove datatype:json from the ajax call.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Make sure your URL is correct. URL should be like this : url: "FileName.aspx/Update_Cart",

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.