jQuery的问题发送JSON数据到ASP.NET的WebMethod数据、问题、JSON、jQuery

2023-09-10 13:22:57 作者:倾心相遇,安暖相陪

我读过所有关于这个问题的问题,但并没有设法解决它...

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

的分数等级:

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

    public int TraitID { get; set; }

    public double TraitScore { get; set; }
}

在aspx的WebMethod:

The ASPX WebMethod:

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

jQuery的code:

The jQuery code:

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

谢谢!

推荐答案

我传递的自定义对象的数组到列表中的Web方法和它工作得很好。

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

我,猜你有,因为周围的属性名称的报价小JSON格式问题。试着改变你的目的是这样的:

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 :

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>

这里的codebehind:

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