简体   繁体   中英

How to convert JavaScript variable to JSON object and insert that JSON object into Mysql using VB.Net

I want to convert my JavaScript variable into JSON object and insert the JSON Object into MySQL database using VB.Net code.

My Code is like this :

Client-Side :

<script type = "text/javascript" >
    $(function () {
        $('#btnSubmit').click(function () {
            var Name = $('#txtname').val();
            var Username = $('#txtUsername').val();
            var Password = $('#txtPassword').val();
            if (Name != '' && Username != '' && Password != '') {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/InsertData",
                    data: "{'Name':'" + Name + "','Username':'" + Username + "','Password':'" + Password  + "'}",
                    dataType: "json",
                    success: function (data) {
                        var obj = data.d;
                        if (obj == 'true') {
                            $('#txtname').val('');
                            $('#txtUsername').val('');
                            $('#txtPassword').val('');
                            $('#lblmsg').html("Details Submitted Successfully");
                            window.location.reload();
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
               });
            }
            else {
                alert('Please enter all the fields')
                return false;
            }
        })
    });
</script>

Server Side :

<WebMethod()> _
Public Shared Function InsertData(ByVal username As String, ByVal subj As String, ByVal desc As String) As String
     Dim msg As String = String.Empty
     Using con As New SqlConnection("server=192.168.1.53;charset=utf8;userid=root;password=password;allow user variables=true;database=information")
         Using cmd As New SqlCommand("insert into userDetails(Name,Username,Password) VALUES(@Name,@Username,@Password)", con)
             con.Open()
             cmd.Parameters.AddWithValue("@Name", Name)
             cmd.Parameters.AddWithValue("@Username", Username)
             cmd.Parameters.AddWithValue("@Password", Password)
             Dim i As Integer = cmd.ExecuteNonQuery()
             con.Close()
             If i = 1 Then
                 msg = "true"
             Else
                 msg = "false"
             End If
         End Using
     End Using
     Return msg
 End Function

But its showing error.. My JSON variables are not declaring in server-side.I'm new to JSON. Please help me. Thanks in advance.

Your javascript code is certainly trying to send JSON but your vb.net code is not looking for json. it's looking for standard post data. This sort of data souldn't really be saved as JSON objects. So your vb.net code is right, let's just change the javascript to send a standard post and your VB.NEt code will be able to do the insert without any problems.

$.ajax({
                type: "POST",
                url: "Default.aspx/InsertData",
                data: {'Name': Name,'Username': Username, 'Password': Password  },
                success: function (data) {
                    var obj = data.d;
                    if (obj == 'true') {
                        $('#txtname').val('');
                        $('#txtUsername').val('');
                        $('#txtPassword').val('');
                        $('#lblmsg').html("Details Submitted Successfully");
                        window.location.reload();
                    }
                },
                error: function (result) {
                    alert("Error");
                }
           });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM