javascript - Send to database using ASP.NET, c# and jQuery -


new ajax here. been trying send database using ajax is not working. in aspx.cs:

[webmethod]     public static void savemsg(string roomcode, string username, string msg)     {         using (sqlconnection con = new sqlconnection(system.configuration.configurationmanager.connectionstrings["lbconnectionstring"].connectionstring))         {             sqlcommand cmd = con.createcommand();              cmd.commandtext = "insert chattable(roomcode, uname, msg) values (" + roomcode + ", '" + username + "', + '" + msg + "')";             cmd.connection = con;             con.open();              cmd.executenonquery();              con.close();         }     } 

i trying insert data using ajax , c# asp.net. aspx file

$.ajax({                     type:'post',                     contecttype: "application/json; charset=utf-8",                     datatype: "json",                     url:"room.aspx?board='" + roomcode + "'",                     data: "{'roomcode':'" + roomcode + "','uname':'" + username + "','msg':'" + <%=message.clientid %> + "'}",                 }) 

the full url http://localhost:1759/room?board='//roomcode'.

is there went wrong? how put url ajax function? in advance!

edit: needed put data type json? new json too...

try code :

javascript :

function getpath() { if (!window.location.origin) {     window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : ''); } var domainpath = window.location.origin + "/"; if (domainpath.indexof("localhost") == -1) {     return domainpath; } else {     return domainpath; } } 

ajax:

you path in getpath() method.

var path = getpath(); $.ajax({                 type:'post',                 contecttype: "application/json; charset=utf-8",                 datatype: "json",                 url: path +"room.aspx?board='" + roomcode + "'",                 data: "{'roomcode':'" + roomcode + "','uname':'" + username + "','msg':'" + <%=message.clientid %> + "'}",             }) 

Comments