将json数据发送到ASP.NET WebMethod的jquery问题

时间:2011-05-26 15:17:27

标签: jquery asp.net ajax json

我已经阅读了有关此问题的所有问题,但没有设法解决它......

分数类:

public class Score
{
    // default constructor
    public Score()
    { }

    public int TraitID { get; set; }

    public double TraitScore { get; set; }
}

ASPX WebMethod:

    [WebMethod]
    public static bool Test(List<Score> scores)
    {
        return true;
    }

jQuery代码:

            var scoresList = [{"TraitID":1,"TraitScore":2}, {"TraitID":2,"TraitScore":5}];

            $.ajax({
                type: "POST",
                url: "Tryouts.aspx/Test",
                data: "{'scores':" + JSON.stringify(scoresList) + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response.d == true) {
                        alert("success!!!!!");
                    }
                    else {
                        alert("problem!!!!!!!!!");
                    }
                },
                error: function () {
                    alert("ERROR");  
                }
            });

我一直收到错误:

{"Message":"Cannot convert object of type \u0027System.String\u0027 to type
\u0027System.Collections.Generic.List`1[BusinessLogicLayer.Score]\u0027","StackTrace":"   at
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type,
JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type,
JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at
System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n
at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target,
IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext
context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,
WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

谢谢!!!

1 个答案:

答案 0 :(得分:6)

我在Web方法中将自定义对象的数组传递给List,它运行得很好。

我猜,由于属性名称周围的引号,你有一个小的JSON格式问题。尝试将您的对象更改为:

var scoresList = [{TraitID:1, TraitScore:2}, {TraitID:2, TraitScore:5}];

并将您的数据行更改为:

data: JSON.stringify({ scores : scoresList }),      

希望有帮助...

更新:工作示例......

<script type="text/javascript">
$(function () {

    var scoresList = [{ TraitID: 1, TraitScore: 2 }, { TraitID: 2, TraitScore: 5}];

    $.ajax({ type: "POST",
        url: "Tryouts.aspx/Test",
        data: JSON.stringify({ scores: scoresList }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d == true) {
                alert("success!!!!!");
            } else {
                alert("problem!!!!!!!!!");
            }
        },
        error: function (xhr) {
            alert("ERROR");
        }
    });

});
</script>

这是代码隐藏:

public class Score
{    // default constructor    
    public Score() { }
    public int TraitID { get; set; }
    public double TraitScore { get; set; }
}

[WebMethod]
public static bool Test( List<Score> scores )
{
    return true;
}