1

I have a webvice asmx that returns an array of item, my problem is that i cannot send some parameters,perhaps without any parameters it works

   <WebMethod>
    Public Function BindMapMarker(Rcodprovincia As String, RcodCitta As String) As MAPS()


 <script type="text/javascript">
        var map;

        function CreateMarker() {

            var params = {
                RcodProvincia: $('#<%=CBB_Provincia.ClientID%> option:selected').val(),
                RcodCitta: $('#<%=CBB_Comune.ClientID%>  option:selected').val() }


            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "WebService.asmx/BindMapMarker",
                data: JSON.stringify(params),//{},//data: JSON.stringify(params),
                dataType: "json",
                success: function (data) 
                { ...},
                error: function (result) {
                    alert("Error");
                }
            });
  • Web service are WSDL in .Net, it will receive and return XML. For Json format better to use Web API. – espino316 Oct 05 '16 at 18:01
  • @espino316 You can return JSON with Soap Webservices: http://stackoverflow.com/questions/11447538/how-to-return-json-from-webservice – NoAlias Oct 05 '16 at 20:18
  • Yes, but it will be returned still between XML tags, and in the $.ajax is expecting JSON, not something preformatted. Also, the web service will be expecting receive XML (SOAP), that's why I think Web API is the way to go, or change you $.ajax to send and receive SOAP – espino316 Oct 05 '16 at 20:49
  • This code looks like this example: http://www.aspsnippets.com/Articles/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet.aspx In the example, the call is to an aspx page, not an asmx web service. To simple aspx page you can send json and return json, because you can manually handle the whole process of request and response. – espino316 Oct 05 '16 at 20:52

1 Answers1

0

In order to send json you must be use a web page (.aspx), not a web service (.asmx) just as the example I mentioned.

You can only pass one parameter and must be an object, an instance of a class, that will be the json parameter.

If your class is MAPS:

Public Class MAPS
    Private _RCodProvincia As String
    Public Property RCodProvincia As String
        Get
            Return _RCodProvincia
        End Get
        Set(ByVal value As String)
            _RCodProvincia = value
        End Set
    End Property
    Private _RCodCitta As Integer
    Public Property RCodCitta As Integer
        Get
            Return _RCodCitta
        End Get
        Set(ByVal value As Integer)
            _RCodCitta = value
        End Set
    End Property
End Class

The your method will be:

<System.Web.Services.WebMethod()> _
    Public Shared Function BindMapMarker(maps As MAPS) As MAPS
       'Do the logic here 
       Return maps
    End Function

And your javascript, you must specify headers to send json (content type) and expect json, in order to jquery form the request correctly:

 $("#btnMAPS").live("click", function () {
            var MAPS = {};
            MAPS.Rcodprovincia = "Mumbai";
            MAPS.RcodCitta = "2000";
            $.ajax({
                type: 'POST',
                url: 'MyPage.aspx/BindMapMarker',
                contentType: 'application/json; charset=utf-8',
                data: "{maps:" + JSON.stringify(MAPS) + "}",
                dataType: 'json',
                success: function (r) {
                      // Do the logic here
                    alert(r);
                    console.log(r);
                },
                error: function (request, status, error) {
                    alert(request.responseText);
                }
            });
        });
espino316
  • 452
  • 4
  • 8