I want to convert my textbox value into json object through javascript variable and after the convertion need to insert the JSON Object into MySQL database using VB.Net code. My Code as follows :
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 Name As String, ByVal UserName As String, ByVal Password As String) As String
Dim msg As String = String.Empty
Dim MySqlConn As MySqlConnection
Dim query As String
Dim dbcomm As MySqlCommand
Dim reader As MySqlDataReader
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=192.168.1.53;charset=utf8;userid=root;password=password;allow user variables=true;database=information"
MySqlConn.Open()
query = "insert into information.userDetails(Name,Username,Password) VALUES(@Name,@Username,@Password)"
dbcomm = New MySqlCommand(query, MySqlConn)
reader = dbcomm.ExecuteReader
Dim i As Integer = dbcomm.ExecuteNonQuery()
If i = 1 Then
msg = "true"
Else
msg = "false"
End If
Return msg
MySqlConn.Close()
End Function
But my json data always throughs error function. It doesn't enter into the success function it always calling error function. Please let me know what to do?? Thanks in advance.
You can use JSON.stringify
to properly send json data
.
Appending quotes and braces
manually requires proper formatting
so if you even miss a single quote error will occur.
var data = JSON.stringify({Name: Name ,Username: Username ,Password:Password});
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/InsertData",
data: data,
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");
}
});
I used all jquery versions but my Jquery function doesn't working and showing error as "Use of getPreventDefault() is deprecated. Use defaultPrevented instead." again again.
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.