Sending - Receiving JSON Objects with ASP.NET -web services or mvc-

We will Assume we are sending and receiving below object.

in VB.NET
Public Class smsWrapper
     Public Property appID As String
     Public Property ph As String
End Class


Same Object in JavaScript
var data = []; // i am creating a array (Which will hold objects)
data.push({"appID":"1234", "ph":"1234"});
data.push({"appID":"4234", "ph":"A1234"});

            $.ajax({
                type: "POST",
                url: "/pages/api/call.aspx/SendSMS",
                data: "{'SmsObject':" + JSON.stringify(data) + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    // use $.each if return type is list
                    alert(data.d.appID) // This Gets the Response Object, .d is required by microsoft
                }
            });


Web Service Code:

' Notice the parameter name is same "SmsObject", and its a LIST ( [] in JS)
    <WebMethod>
    Shared Function SendSMS(ByVal SmsObject As List(Of sms.objects.smsWrapper)) As sms.objects.smsWrapper
        Dim s As New sms.objects.smsWrapper
        s.appID = "APP432"
        s.ph = "3121"
        Return s
    End Function
Posted by:

Comments History


by: on
and if web service is returning array / list

    <WebMethod>
    Shared Function SendSMS(ByVal SmsObject As List(Of sms.objects.smsWrapper)) As sms.objects.smsWrapper()

        Dim s As sms.objects.smsWrapper() = {
                                             New sms.objects.smsWrapper With {.appID = "sdsd", .ph = "dsdsd"},
                                             New sms.objects.smsWrapper With {.ph = "ddsd", .appID = "dsdsd"}
                                            }

        Return s

    End Function


in JS


            $.ajax({
                type: "POST",
                url: "/pages/api/call.aspx/SendSMS",
                data: "{'SmsObject':" + JSON.stringify(data) + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    console.log(data)
                    // alert(data.d.appID)
                    $.each(data.d, function (index, value) {
                        alert(value.appID)
                        }
                    )
                }
            });



 

Name :  

Email :  

Comment Below