I have a little problem with posting my JavaScript object to the server (asp.net 4.5 c#).
the object is created by user (i´m using jquery for that), and then user shod be able to send that object to server to process the information,
the data i need to send to the server is array of jQuery objects,
I´ve already tried this but it didnt worked :
var sendOrder = function () {
$.ajax({
type: "POST",
url: "Products.aspx/ProcessOrder",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
data: JSON.stringify(addedProducts),
success: function (result) {
debugger;
alert(result);
},
error: function (result) {
debugger;
alert(result.responseText);
}
});
}
sendOrderInfo.bind('click', sendOrder);
server side method is :
[System.Web.Services.WebMethod]
public static string ProcessOrder(object orderInfo) {
return "Order Processed Successfully";
}
and the object (array of objects) posted looks like this :
addedProducts
[
Object
product: "Cerejas"
quantity: "45"
__proto__: Object
,
Object
product: "Farinheira"
quantity: "4"
__proto__: Object
]
Does anyone have some idea?
I hope you are following this process :
creating a class for the Objects
public class Products
{
public string product { get; set; }
public int quantity { get; set; }
}
[System.Web.Services.WebMethod]
public bool ProcessOrder(Products orderInfo)
{
return orderInfo != null;
}
json call :
$.ajax({
url: "Products.aspx/ProcessOrder",
type: "POST",
contentType: "application/json",
data: JSON.stringify(addedProducts),
success: function(response) {
response ? alert("It worked!") : alert("It didn't work.");
}
});
Do let me know if it works for you.