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.

asked Feb 2, 2015 at 11:16
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.

answered Feb 2, 2015 at 11:28
1
  • Make sure your URL is correct. URL should be like this : url: "FileName.aspx/Update_Cart", Commented Feb 2, 2015 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.