使用jQuery和AJAX传递参数给VB.NET的webmethod [英] Using Jquery and AJAX to pass parameters to VB.NET webmethod
本文介绍了使用jQuery和AJAX传递参数给VB.NET的webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在网上搜索了几个小时试图将参数传递到使用jQuery $就背着我code。我试过一吨不同的东西,但没有奏效。当我不传递任何参数,并设定vb.net功能不接收参数的功能将被调用。但是,一旦我尝试添加参数,函数永远不会被调用。
客户端:
$(#<%= saveResource2.clientID%>中)。点击(函数(){
VAR parDesc = $(#<%= ddlPDesc.clientID%>选项:选择)。text()的;
$(#<%= Button1.clientID%>中)。点击();
$阿贾克斯({
键入:POST,
网址:Projects.aspx / btnSaveResource
数据:JSON.stringify({说明:parDesc}),
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
成功:函数(MSG){
$(#<%= lblPerson.clientID%>中)。文(MSG);
//做一些有趣的事情在这里。
}
});
});
服务器端:
<的WebMethod()> _ < ScriptMethod(ResponseFormat:= ResponseFormat.Json)> _ 公共共享功能btnSaveResource(BYVAL parDesc作为字符串)作为字符串 昏暗的Ð作为字符串= parDesc 返回D +测试 端功能
解决方案
尝试从这个不断变化的:
数据:JSON.stringify({说明:parDesc}),
要
数据:JSON.stringify({parDesc:parDesc}),
I've been searching the internet for hours trying to pass parameters to my code behind using JQUERY $.ajax. I've tried a ton of different things, but nothing has worked. When I don't pass any parameters and set the vb.net function to not receive parameters the functions will get called. But once I try adding parameters, the function never gets called.
Client Side:
$("#<%=saveResource2.clientID %>").click(function() {
var parDesc = $("#<%=ddlPDesc.clientID %> option:selected").text();
$("#<%=Button1.clientID %>").click();
$.ajax({
type: "POST",
url: "Projects.aspx/btnSaveResource",
data: JSON.stringify({Desc: parDesc}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#<%=lblPerson.clientID %>").text(msg);
// Do something interesting here.
}
});
});
Server Side:
<WebMethod()> _ <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ Public Shared Function btnSaveResource(ByVal parDesc As String) As String Dim d As String = parDesc Return d + "test" End Function
解决方案
Try changing from this:
data: JSON.stringify({Desc: parDesc}),
To
data: JSON.stringify({parDesc: parDesc}),
这篇关于使用jQuery和AJAX传递参数给VB.NET的webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文