【问题标题】:Call Webmethod by ref/out parameters in javascript通过 javascript 中的 ref/out 参数调用 Webmethod
【发布时间】:2019-04-24 19:59:02
【问题描述】:

这是我的webmethod

[WebMethod]
    public string CheckService(string name, ref string msg)
    {
        return "Hello" + name;
    }

这是我的ajax 电话

$(document).ready(function () {
                $.ajax({
                    type: "POST",
                    url: "<%= ResolveUrl("integrator.asmx/CheckService") %>",
                    data: '{name: "zakki",msg:"" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (data1) {
                        console.log('', data1.d);
                    }
                });
            });

它会引发错误

Cannot convert object of type 'System.String' to type 'System.String&'

【问题讨论】:

  • WebMethod 中的所有参数都必须使用传值,您不能为此使用ref(和out)关键字。
  • ref 在你的方法中有什么意义?

标签: c# asp.net ajax web-services webforms


【解决方案1】:

简短的回答是:您不能对标有[WebMethod] 属性的方法中的参数使用传递引用(使用ref/out 关键字)。

这就是为什么&amp; 添加第二个System.String 输入异常消息的原因,取自this reference

解码此消息来源的关键是位于 文本的结尾。这是传递参数时使用的语法 到函数 BYREF (By-Reference) & 用于 C-Notation 表示“传递 AddressOf”变量名。即使你编码 C#/VB.NET 中的 WebService,微软会将其转换/编译为 C 类型 将其作为程序集输出时的符号。

因此,使用[WebMethod] 属性的方法内的所有参数都必须使用传值,方法是删除ref 关键字:

[WebMethod]
public string CheckService(string name, string msg)
{
    // return string here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-11
    • 2016-08-07
    • 2011-07-26
    • 2012-03-01
    • 1970-01-01
    • 2023-03-31
    • 2012-08-08
    相关资源
    最近更新 更多