2

我收到以下错误:

"Message":"No parameterless constructor defined for type of \u0027System.String\u0027.","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   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.MissingMethodException"}

我假设发生这种情况是因为我没有正确地从我的 ajax 帖子中传递一些东西,但我看不到它:(。如果我没有传递任何参数,并且我从 web 方法中删除了参数,它就可以工作。但是,对于它们,它不会。此外,如果我尝试传递 JSON 对象,它也不起作用。我在底部发布了该代码。如果有人可以帮助我,我将不胜感激!谢谢。

AJAX

$.ajax({
                    type: "POST",
                    url: 'Process.aspx/updateAProcess',
                    data: JSON.stringify({ 'ProcessID': ProcessID, 'ProcessDescription': ProcessDescription, 'ProcessName': ProcessName }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        alert("Winner!");
                    }
                });

网络方法

<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function updateAProcess(ByVal ProcessID As String, ByVal ProcessDescription As String, ByVal ProcessName As String)
    Dim pBSL As New ProcessBSL
    pBSL.ProcessUpdate(ProcessID, ProcessDescription, ProcessName)
    Return Nothing


End Function

使用 JSON

这是我的数据调用: data: JSON.stringify({ updateProcess: updateProcess }),

这里是我创建 updateProcess 的地方:

                var updateProcess = new Object();
                updateProcess.ProcessName = $("#ProcessNameChange").val();
                updateProcess.ProcessDescription = $("#ProcessDescChange").val();
                updateProcess.ProcessID = $(this).closest("tr").children()[4];

它导致此错误: {"Message":"Invalid web service call, missing value for parameter: \u0027ProcessID\u0027.","StackTrace"

在后端,我有一个名为 ProcessBO 的对象,它有十几个属性,我的 webmethod 是:

    Public Shared Function update(ByVal updateProcess As ProcessBO)
    Dim pBSL As New ProcessBSL
    pBSL.ProcessUpdate(updateProcess.ProcessID, updateProcess.ProcessDescription, updateProcess.ProcessName)
    Return Nothing
4

1 回答 1

2

出现错误消息是因为 ASP.Net 尝试解码参数时发生异常。不知何故,它的内部代码最终试图反序列化一个异常。

问题的根源在于您将字符串参数作为纯文本传递,而不是将它们封装在引号中。例如,如果 ProcessID = "1"、ProcessDescription = "2" 和 ProcessName = "3",您的参数将如下所示:

{ 'ProcessID': 1, 'ProcessDescription': 2, 'ProcessName': 3 }

但它们应该看起来像

{ 'ProcessID': '1', 'ProcessDescription': '2', 'ProcessName': '3' }

我发现实现这一点最简单和最安全的方法是实际创建一个带有相应参数的 js 类,用数据填充它,然后将这个类传递给 stringify:

var params = new Object();
params.ProcessID = ProcessID;
params.ProcessDescription = ProcessDescription;
params.ProcessName = ProcessName;

$.ajax({
    type: "POST",
    url: 'Process.aspx/updateAProcess',
    data: JSON.stringify(params),
...
于 2013-07-26T18:17:32.347 回答