0

I have the following code in jquery

            $('#btnSubmit').click(function () {
            $.ajax({
                type: "POST",
                url: "Appointment.aspx/saveAppointment",
                data: "{firstname:'" + firstname + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(1);
                    // Do something interesting here.
                }
            });
        });

I am calling this function in vb.net

<WebMethod()> _
Public Shared Function saveAppointment(ByVal firstname As String) As Boolean

    Dim checkval = globalclass.firstname
    Try

    Catch ex As Exception
        Throw ex
    End Try
    Return True
End Function

End Class

It seems to work without any parameters. There is no call if the parameters are provided. I also referred this but does seem to work

Calling an ASP.NET server side method via jQuery

Thanks!

Community
  • 1
  • 1
Shephed
  • 11
  • 1
  • 4

1 Answers1

0

https://api.jquery.com/jQuery.ajax/

to post data as object:

data Type: PlainObject or String Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

data: {firstname: firstname} 

or

data: {firstname: firstname}  // in case you want to assign "firstname" as string value        to the data you are posting.

considering firstname on the right side is a defined variable in your javascript , while the firstname on left side is the name you will use in the other side script ( PHP , vb.net ... etc )

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • I tried that but even this didn't work. I most used the above method when I worked. Used it assign it entities in separate file. The following code seems to be working - data: '{firstname:'+ '"' + firstname + '"' + '}', – Shephed Apr 20 '14 at 21:57
  • @user2165764 im not experienced with vb.net but seems that the problem is with your vb script even if it works , the ajax data syntax is invalid. Unless you are posting the whole string for example if firstname = "xxx" , then you must be posting : "{firstname:'xxx'}" – ProllyGeek Apr 20 '14 at 21:59
  • I think the Vb is fine. Even for the function with function with no parameters I had to pass the values as Data: "{}" ; else the function wasn't called. I am kind of confused now as that's not how I remember jquery. Ideally your code should have worked. Still looking. Will update here if I find a reason. – Shephed Apr 20 '14 at 22:17
  • if you pass data as "{}" , it means you are passing a string not an object. – ProllyGeek Apr 20 '14 at 22:18