gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:08:22 26 4
gpt4 key购买 nike

我已阅读有关此问题的所有问题,但未能解决它...

分数类:

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

谢谢!!!

最佳答案

我将自定义对象数组传递到 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;
}

关于jquery 将 json 数据发送到 ASP.NET WebMethod 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6140940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com