JavaScript:
var paramStr = $('#id1').val() + '|' + $('#id2').val() + '|' + $('#id3').val();
paramStr = '{"searchCriteria": "' + paramStr + '"}';
$.ajax({
type: "POST",
url: "/MyService.asmx/MyWebMethod",
data: paramStr,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
}
});
VB.NET:
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class LogisticsMainMenuService
Inherits System.Web.Services.WebService
<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function MyWebMethod(ByVal searchCriteria As String) As String
Dim dummy as Integer = 1
.
.
Return someResult
End Function
End Class
I put a break point inside MyWebMethod. When I invoke this call on the page, the break point never gets hit. It works fine when I remove all parameters from MyWebMethod’s signature and pass in ‘{}’ from JS as parameters. Once I try to pass in a string parameter, it stops working.
Ok! After seriously banging my head on the keyboard and picking up some extra FireBug skills, I got my problem solved.
url: “/MyService.asmx/MyWebMethod”,
should be
url: “MyService.asmx/MyWebMethod”,
without the first “/”.
Apparently, the first call is going to http://www.mysite.com/MyService.asmx as oppose http://www.mysite.com/dir/MyService.asmx even if making the call from within /dir… which I did.
Happy AJAXing, everybody!