Reputation: 49
i can records to database using jquery and webmethod But Success method could never get invoked... Alert message never shown...
function d(t) {
e.ajax({
url: "productmodel.aspx/AddProductToCart",
type: "POST",
data: JSON.stringify(t),
dataType: "json",
contentType: "application/json; charset=utf-8",
sucess: function () {
alert("added to cart successfully");
}
})
}
[WebMethod]
public static void AddProductToCart(int productid)
{
MyShoppingCart usersShoppingCart = new MyShoppingCart();
String cartId = usersShoppingCart.GetShoppingCartId();
try
{
usersShoppingCart.AddItem(cartId, productid, 1);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Upvotes: 0
Views: 54
Reputation: 4185
If you can record the data to DB just not getting the response, you can modify the void method to return HttpResponseMessage back to page with customized error handling message. As for the Ajax call, try this:
function d(t) {
var isSuccess = false;
e.ajax({
url: "productmodel.aspx/AddProductToCart",
type: "POST",
data: JSON.stringify(t),
dataType: "json",
async:false,
contentType: "application/json; charset=utf-8",
success: function () {
isSuccess = true;
},
error: function (textStatus, errorThrown) {
isSuccess = false;
}
});
alert(isSuccess);
return isSuccess;
}
Upvotes: 1
Reputation: 25537
Try like this
$.ajax({
url: "productmodel.aspx/AddProductToCart",
type: "POST",
data: {productid : 1 },
dataType: "json",
sucess: function () {
alert("added to cart successfully");
}
});
Structure of t
in your code should look like { productid : 1 }
Upvotes: 1