Can';t获取带有参数的ajax调用以命中我的C#函数
阿贾克斯: 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);
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);
}
});
});你能从邮递员那里得到它吗?请在Chrome(网络选项卡)中加载开发人员工具,并向我们显示正在发出的请求(包括URL和发送的有效负载)。我对ajax和jquery不熟悉,因此我还不熟悉最佳实践。您尝试过json.stringify吗?数据:JSON.stringify({input:test}),您从哪个页面发送请求?@ChasetopherB P.S.我忘记添加了,您知道jquery ajax中没有“failure”这样的函数吗?出现问题时指定回调的正确选项是“error”。目前,当方法失败时,代码将不起任何作用。此外,“error”回调在响应中不包含名为d
的属性,而是会得到一个XHR对象——文档中解释了它的内容。我经常在这里看到这个错误,也许网上有一些垃圾教程在使用它。您可以在此处检查所有有效选项等:
contentType: "application/json; charset=utf-8"