0

New to AJAX here. Been trying to send to my database using AJAX but is is not working. In my 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 into chatTable(roomCode, uName, msg) VALUES (" + roomCode + ", '" + userName + "', + '" + msg + "')";
            cmd.Connection = con;
            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();
        }
    }

I am trying to insert data using AJAX and C# ASP.NET. This is my 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 is http://localhost:1759/Room?Board='//roomcode'.

Is there anything that went wrong? Like how I put the url into the AJAX function? Thanks in advance!

EDIT: Is it needed to put data type as JSON? New to JSON too...

  • you shouldn't concatenate string while passing parameters. For example, data: { roomCode: roomCode, uName: userName, msg: message.ClientID }. Read similar question http://stackoverflow.com/questions/1916309/pass-multiple-parameters-to-jquery-ajax-call – kashi_rock Aug 01 '16 at 11:43
  • @kashi_rock but my data need user input can just put like tat? sorry new here... :) – Centrifudge Aug 01 '16 at 11:45
  • Please take a look here [http://stackoverflow.com/questions/7081054/calling-a-function-database-update-using-ajax-via-jquery/40427446#40427446](http://stackoverflow.com/questions/7081054/calling-a-function-database-update-using-ajax-via-jquery/40427446#40427446) for a similar question if it helps.. – Jeff D Nov 04 '16 at 16:29

2 Answers2

1

Try this 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 will get the 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 %> + "'}",
            })
Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
-1

You Need to Pass correct URL to call method.

$.ajax({
       type:'POST',
       contectType: "application/json; charset=utf-8",
       dataType: "json",
       url:"Room.aspx/saveMsg",
       data: "{'roomCode':'" + roomCode + "','uName':'" + userName + "','msg':'" + <%=message.ClientID %> + "'}",
   })
DBB
  • 137
  • 1
  • 4