0

我已将代码简化为仅传递数组,但仍然没有任何运气
当我单步执行代码并到达 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 解决方案有两个原因:

  1. 当我向 ajax 请求添加参数时,它消除了意外拼写错误的危险。
  2. 根据 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
}

再次感谢您的所有帮助

4

4 回答 4

1

在此处此处此处查看最接近的答案。许多人犯了同样的错误。

  1. 在 JavaScript 对象的初始化中,您可以同时使用引号和双引号,但在 JSON 数据中只允许使用双引号。
  2. 您不应该像{"{'startDate':'" + startDate + "', ...使用旧的$.toJSONjQuery 插件一样制作 JSON 序列化手册。最好的方法是使用json2.js中的JSON.stringify函数。最新版本的网络浏览器支持本机代码中的功能,因此运行速度非常快。

另一件看起来很奇怪的事情是路径“/myurl/jquery.aspx/GenerateUserSchedules”而不是“/myurl/jquery.asmx/GenerateUserSchedules”(asmx 而不是 aspx)。

在您的情况下,您应该使用

data: JSON.stringify({
    startDate: startDate,
    endDate: endDate,
    ddlViewSelectedItem: ddlViewSelectedItem,
    ddlViewSelectedValue: ddlViewSelectedValue,
    ddlOrgSelectedValue: ddlOrgSelectedValue
})

在使用type: "POST"和的情况下

data: {
    startDate: JSON.stringify(startDate),
    endDate: JSON.stringify(endDate),
    ddlViewSelectedItem: JSON.stringify(ddlViewSelectedItem),
    ddlViewSelectedValue: JSON.stringify(ddlViewSelectedValue),
    ddlOrgSelectedValue: JSON.stringify(ddlOrgSelectedValue)
}

如果您决定使用type: "GET".

为 web 方法的所有输入参数发送数据很重要。至少您应该发送带有null值作为输入的参数(对于可为空的对象)。

更新:当时你重写了你的问题。所以现在你的问题的新版本的答案。

你应该使用

$.ajax({  
    type: "POST",  
    url: "/myurl/jquery.aspx/GenerateUserSchedules",  
    data: JSON.stringify({data: [
            {UserId:8},{UserId:9},{UserId:5},{UserId:13},{UserId:6},{UserId:11}]}),  
    contentType: "application/json",  
    dataType: "json",  
    success: function () { alert('Made It!'); },  
    error: function (result) { alert(Failed: ' + result.responseText);   
});

原因很简单。因为您有GenerateUserSchedules(User[] data)带有data输入参数的方法,所以您应该将JSON.stringify({data: yourData})其用作输入。对象数组User应该是包含项目{UserId:8}(或{'UserId':8}{"UserId":8})的数组,但不是{UserId:"8"}

于 2011-03-08T17:00:52.287 回答
1

确保 json 属性名称和类型与 Web 方法参数匹配。您的 jsonText 变量是一个数组,因此 Web 方法需要一个数组类型的属性来接受它(就像 Nikhil 发布的示例一样)。

因此,如果您在 Nikhil 的示例中使用 Web 方法签名和自定义用户对象,则需要将 jquery ajax 调用的 data 属性设置为:

"{'startDate':'" + startDate + "', 'endDate':'" + endDate + "', 'ddlViewSelectedItem':'" + ddlViewSelectedItem + "', 'ddlViewSelectedValue':'" + ddlViewSelectedValue + "', 'ddlOrgSelectedValue':'" + ddlOrgSelectedValue + "','customObejct':" + jsonText + "}"
于 2011-03-08T16:29:11.757 回答
1

可以这么简单吗:

data: "{'data':'" + jsonTextSerialized + "'}",

改成

data: '{"data":"' + jsonTextSerialized + '"}',

和/或将客户端更改"UserId""UserID"

于 2011-03-08T16:48:05.643 回答
1

有几种方法可以做到这一点。如果您的应用程序使用 MS Ajax 库,那么您需要在客户端做的就是

var jsonText = [{"UserId":"8"},{"UserId":"9"},{"UserId":"5"}];
var jsonTextSerialized = Sys.Serialization.JavaScriptSerializer.serialize(jsonText);

然后,您可以将此 jsonTextSerialized 与您必须将其发送到服务器端代码的其他参数一起使用。在服务器端,您可以拥有

public class User{
  public Int32 UserId{
    get;set;
  }
}
[System.Web.Script.Services.ScriptMethod]  
    [System.Web.Services.WebMethod]  
    public static void GenerateUserSchedules(string startDate, string endDate, string ddlViewSelectedItem, string ddlViewSelectedValue, string ddlOrgSelectedValue, User[] customObejct) 

这应该会自动为您完成。

如果您不使用 MS Ajax,请从此处获取 JSON2.js

http://www.json.org/js.html

您可以使用此库在客户端序列化您的对象。在服务器端,事情应该保持不变。

有关更详细的指南和信息,请查看此

http://forums.asp.net/t/1388935.aspx

希望这可以帮助!

尼基尔

您的自定义对象现在应该具有该对象

于 2011-03-08T16:01:56.247 回答