Can';t获取带有参数的ajax调用以命中我的C#函数

Can';t获取带有参数的ajax调用以命中我的C#函数,c#,jquery,asp.net,ajax,webmethod,C#,Jquery,Asp.net,Ajax,Webmethod,阿贾克斯: C#功能: var test = "test"; $.ajax( { type: "POST", url: "project/function", contentType: "application/json; charset=utf-8", dataType: "json", data: { input: test }, success: function (response) { $("#lblMsg").text(response.d);

阿贾克斯:

C#功能:

var test = "test";

$.ajax(
{
  type: "POST",
  url: "project/function",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: { input: test },
  success: function (response) {
    $("#lblMsg").text(response.d);
  },
  failure: function (response) {
    alert(response.d);
  }
});
当我不包含参数时,连接成功。我尝试了ajax调用的“数据”部分的不同单引号和双引号排列,但都没有用

我还尝试将数据类型设置为“text”,结果类似


我遗漏了什么?

我建议您不要将数据作为JSON发送。只需移除

[WebMethod]
public void function(string input)
{
}

jQuery将把数据序列化为正常的url编码格式的数据格式,这正是WebMethod所期望的格式。

试试这个可能会解决您的问题

var test=“test”

$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“项目/功能”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
数据:JSON.stringify({'input':test}),
成功:功能(响应){
$(“#lblMsg”).text(response.d);
},
故障:功能(响应){
警报(response.d);
}
});

});d
的属性,而是会得到一个XHR对象——文档中解释了它的内容。我经常在这里看到这个错误,也许网上有一些垃圾教程在使用它。您可以在此处检查所有有效选项等:
contentType: "application/json; charset=utf-8"