c# - 通过 javascript 中的 ref/out 参数调用 Webmethod
问题描述
这是我的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&'
解决方案
简短的回答是:您不能对标有属性的方法中的参数使用传递引用(使用ref
/out
关键字) 。[WebMethod]
这是解释为什么在异常消息中&
添加第二种System.String
类型的原因,取自此参考:
解码此消息来源的关键是位于文本末尾的“&”。这是将参数传递给函数 BYREF(By-Reference)时使用的语法,并在 C-Notation 中用于指示“传递 AddressOf”变量名称。即使您在 C#/VB.NET 中编写了 WebService,Microsoft 也会在将其作为程序集输出时以 C 类型表示法对其进行转换/编译。
因此,使用[WebMethod]
属性的方法中的所有参数都必须使用按值传递,通过删除ref
关键字:
[WebMethod]
public string CheckService(string name, string msg)
{
// return string here
}
推荐阅读
- java - 尝试求解二次方程时,Math.sqrt 在 Eclipse 中显示无法解释的错误
- r - 如何使用 GGPLOT 自动调整时间刻度(x 轴)限制?
- javascript - for循环遍历香草javascript中的元素-只有手风琴中的第一个元素有效
- typescript - 使用 turf 创建 LayerGroup 的联合会引发 TypeError
- r - GLM:具有少数状态作为因子或数字的连续变量?
- kubernetes - 如何更新使用 Kubernetes 清单创建的所有资源?
- python-3.x - 根据条件更改熊猫数据框子组上特定列的值
- python - 使用循环创建文件夹
- python - 如何在不使用 python 库的情况下实现 Tf-idf?
- javascript - 防止 JavaScript 中元素的样式更改