AJAXを使用してJSON配列を.NETWebサービスに渡します。文字列化されたJSON配列は、送信時に正しいです-コンテンツをJSアラートで表示します-しかし、Webサービス内で出力すると、null値のみが表示されます。
ヘルプ。
JSはこれです:
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);
}
});
受信サービスは次のとおりです。
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;
}
}
入るStringyfiedフィールドは次のとおりです。
{"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は次のように返されます:
{ "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" }
どんな助けでも大歓迎です。ありがとう。