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

时间:2021-10-24 16:37:07

I've read all the questions regarding this issue but didn't manage to solve it...

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

The Score class:

分数类:

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

    public int TraitID { get; set; }

    public double TraitScore { get; set; }
}

The ASPX WebMethod:

ASPX WebMethod:

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

The jQuery code:

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");  
                }
            });

I keep getting the 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"}

Thanks !!!

谢谢 !!!

1 个解决方案

#1


6  

I'm passing arrays of custom objects into List in web methods and it works just fine.

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

I'm, guessing that you're having a small JSON formatting issue because of the quotes around the property names. Try changing your object to this :

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

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

and change your data line to this :

并将您的数据行更改为:

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

Hope that helps...

希望有帮助......

UPDATE: working example...

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

<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>

Here's the codebehind :

这是代码隐藏:

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;
}

#1


6  

I'm passing arrays of custom objects into List in web methods and it works just fine.

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

I'm, guessing that you're having a small JSON formatting issue because of the quotes around the property names. Try changing your object to this :

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

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

and change your data line to this :

并将您的数据行更改为:

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

Hope that helps...

希望有帮助......

UPDATE: working example...

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

<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>

Here's the codebehind :

这是代码隐藏:

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;
}