I am using AJAX to pass a JSON Array to a .NET web service. The Stringified JSON array is correct when sent - I show the content in a JS alert - but when I output it within the web service, it just displays null values.
Help.
The JS is this:
var listitems = [];
$('#job-item-list').children('.iamalistitem').each(function () {
listitems.push({ "title": $(this).find('.job-item-title').text(), "price": $(this).find('.job-item-price').text().replace("£","").replace("£","") });
});
alert(JSON.stringify({ ListItems: listitems }));
$.ajax({
type: "POST",
url: "/ld_jobs.asmx/putJobItems",
// The key needs to match your method's input parameter (case-sensitive).
data: JSON.stringify({ ListItems : listitems }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var responseObjTwo = JSON.parse(response.d);
//showUserMessage(responseObjTwo.Message, false);
showUserMessage(response.d, false);
},
error: function (response) {
var responseObj = JSON.parse(response.responseText);
showUserMessage(responseObj.Message, true);
}
});
The receiving service is this:
public class ListItem
{
public string itemTitle { get; set; }
public decimal itemPrice { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string putJobItems(ListItem[] ListItems)
{
// Add to DB
int errorNumber = 0;
try
{
// check if on DB already
string returnstring = "";
errorNumber++; //1
foreach (ListItem item in ListItems)
{
errorNumber++; //2,3,4
returnstring += "Desc: " + item.itemTitle + "; Price: " + item.itemPrice.ToString() + "; ";
}
errorNumber++; //5
return "{ \"Status\" : \"Success\", \"Message\" : \"Your request has been successfully posted. Well done you. Go put the kettle on and be lazy! \", \"input\" : \"" + returnstring + ": " + errorNumber.ToString() + "\" }";
}
catch (Exception ex)
{
Exception newEx = new Exception("Well this is quite embarassing. It seems the badgers have escaped! (Actual error: " + ex.Message + "; error: " + errorNumber.ToString() + ")");
throw newEx;
}
}
The Stringyfied field going in is:
{"ListItems":[{"title":"(Groceries) 4 pints of milk","price":"2.00"},{"title":"(Groceries) 2 loafs of bread","price":"3.00"},{"title":"(Subway) Foot Long BMT","price":"3.00"}]}
Response.d comes back as:
{ "Status" : "Success", "Message" : "Your request has been successfully posted. Well done you. Go put the kettle on and be lazy! ", "input" : "Desc: ; Price: 0; Desc: ; Price: 0; Desc: ; Price: 0; : 5" }
Any help greatly appreciated. Thanks.
asked Aug 26, 2012 at 18:14
-
have you debug it and check that you are getting data in your listItem in web service ?Waqar Janjua– Waqar Janjua2012年08月26日 18:23:50 +00:00Commented Aug 26, 2012 at 18:23
-
I have and the values are not there, but what I can't work out is that fact that it always has the correct number of items in the ListItem[] array, but they are all null...I can't figure out why it would not be receiving the actual data, but still have the correct number of items in the list?Ben Drury– Ben Drury2012年08月26日 18:27:47 +00:00Commented Aug 26, 2012 at 18:27
1 Answer 1
You are use title
and price
properties names on client but itemTitle
and itemPrice
on server. Adjust them to use same names.
answered Aug 26, 2012 at 18:41
-
Just figured that out, as you posted! Rookie error! Thanks. All working now! ** Little Embarrassed **Ben Drury– Ben Drury2012年08月26日 18:47:34 +00:00Commented Aug 26, 2012 at 18:47
default