我正在尝试使用 jquery ajax 将数组传递给 c# webmethod。
下面的代码在 C# 4.0 中运行良好,但在 c# 2.0 中创建了以下异常。任何人都知道如何解决这个问题?
javascript代码
var myarray=new Array();
$.ajax({
type: "POST",
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: "ClientSideMethods.asmx/ArrayTest",
data: { "values": JSON.stringify(myarray) }
}
).complete(function (data) {
alert('complete');
});
c# 代码
[WebMethod(EnableSession = true)]
public void ArrayTest(List<string> values)
{
...
}
异常(来自萤火虫)
System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
编辑: 如果我删除contentType: "application/json; charset=utf-8",
帖子将起作用,但值不会作为数组接收
编辑:当我将内容类型更改contentType: "application/json
为以下错误时
System.InvalidOperationException: AnywhereLogin Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)