Asp.net 为什么我的C#web服务没有获取我的JSON数组数据?
我使用AJAX将JSON数组传递给.NETWeb服务。Stringified JSON数组在发送时是正确的-我在JS警报中显示内容-但是当我在web服务中输出它时,它只显示空值 救命啊 JS是这样的:Asp.net 为什么我的C#web服务没有获取我的JSON数组数据?,asp.net,json,Asp.net,Json,我使用AJAX将JSON数组传递给.NETWeb服务。Stringified JSON数组在发送时是正确的-我在JS警报中显示内容-但是当我在web服务中输出它时,它只显示空值 救命啊 JS是这样的: var listitems = []; $('#job-item-list').children('.iamalistitem').each(function () { listitems.pu
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;
}
}
进入的严格化现场是:
{"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"}]}
答复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" }
非常感谢您的帮助。谢谢。您在客户端使用
title
和price
属性名称,但在服务器上使用itemttitle
和itemPrice
。调整它们以使用相同的名称。是否调试并检查是否在web服务中的listItem中获取数据?我有,但值不存在,但我无法确定的是,它在listItem[]数组中始终具有正确的项数,但是它们都是空的…我不明白为什么它不会收到实际的数据,但是列表中仍然有正确的项目数?正如你发布的那样,我已经知道了!新手失误!谢谢现在一切正常!**有点尴尬**