我已将代码简化为仅传递数组,但仍然没有任何运气
当我单步执行代码并到达 ajax 请求的要点时
jsonText 包含:
[{"UserId":"8"},{"UserId":"9"},{"UserId":"5"},{"UserId":"13"},{"UserId":"6"},{"UserId":"11"}]
和
jsonTextSerialized contains:
"[{\"UserId\":\"8\"},{\"UserId\":\"9\"},{\"UserId\":\"5\"},{\"UserId\":\"13\"},{\"UserId\":\"6\"},{\"UserId\":\"11\"}]"
function GetUserSchedules() {
var jsonText = $.toJSON(arrParams);
var jsonTextSerialized = Sys.Serialization.JavaScriptSerializer.serialize(jsonText);
$.ajax({
type: "POST",
url: "/myurl/jquery.aspx/GenerateUserSchedules",
data: "{'data':'" + jsonTextSerialized + "'}",
contentType: "application/json",
dataType: "json",
success: function () { alert('Made It!'); },
error: function (result) { alert(Failed: ' + result.responseText);
});
我背后的代码有
[Serializable]
public class User
{
public int UserId { get; set; }
}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static void GenerateUserSchedules(User[] data)
{
//do stuff
}
responseText 是:
“处理请求时出错。”,“StackTrace”:“”,“ExceptionType”:“”}
我究竟做错了什么?
我在您的帮助下的解决方案:
谢谢大家的努力。我无法表达对您的所有投入的感激之情。我很尴尬地承认这一点,但我已经坚持了好几天了。
我从您的所有答案中看到,有多种方法可以解决此问题。我最喜欢 JSON.stringify 解决方案有两个原因:
- 当我向 ajax 请求添加参数时,它消除了意外拼写错误的危险。
- 根据 Oleg 的说法,序列化数据对象是一种更有效的方法
所以这就是我决定解决这个问题的方法。
<script type="text/javascript">
var startDate;
var endDate;
var ddlViewSelectedItem;
var ddlViewSelectedValue;
var ddlOrgSelectedValue;
var arrUsers= [];
$(document).ready(function () {
ddlViewSelectedItem = $('#<%=ddlView.ClientID %> option:selected').text();
ddlViewSelectedValue = $('#<%=ddlView.ClientID %> option:selected').val();
ddlOrgSelectedValue = $('#<%=ddlOrganization.ClientID %> option:selected').val();
$.when(GetStartDate(), GetEndDate()) //these populate strt and end dates
.then(function () {
GetUserIDs(); // populates arrUsers
GetUserSchedules();
})
.fail(function () {
failureAlertMsg();
})
});
// Here I use JSON.stringify because it simplifies adding params. No messy single and/or double quote confusion. I love this. Must include json2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js
function GetUserSchedules() {
var jsonTextStringified = JSON.stringify({ data: arrParams, startDate: startDate, endDate: endDate, ddlViewSelectedItem: ddlViewSelectedItem, ddlViewSelectedValue: ddlViewSelectedValue, ddlOrgSelectedValue: ddlOrgSelectedValue });
$.ajax({
type: "POST",
url: "/myurl/jquery.aspx/GenerateUserSchedules", // this is a call to a pagemethod, not a webservice, so .aspx is correct
data: jsonTextStringified,
contentType: "application/json",
dataType: "json",
success: function () { alert('Sweet! Made It!'); }
,
error: function (result) { alert('Failed!: ' + result.responseText); }
});
}
后面的代码:
[Serializable]
public class User
{
public string UserId { get; set; }
}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static void GenerateUserSchedules(User[] data, string startDate, string endDate, string ddlViewSelectedItem, string ddlViewSelectedValue, string ddlOrgSelectedValue)
{
//do cool stuff and eventually send data back
}
再次感谢您的所有帮助