使用ASP.NET,c#和jQuery发送到数据库

时间:2021-03-11 18:16:00

New to AJAX here. Been trying to send to my database using AJAX but is is not working. In my aspx.cs:

这里是AJAX的新手。一直试图使用AJAX发送到我的数据库但是没有用。在我的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和C#ASP.NET插入数据。这是我的aspx文件

$.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'.

完整的网址是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!

有什么问题吗?就像我把url放入AJAX函数一样?提前致谢!

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

编辑:是否需要将数据类型设置为JSON? JSON的新手......

2 个解决方案

#1


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.

您将在Getpath()方法中获取路径。

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 %> + "'}",
            })

#2


-1  

You Need to Pass correct URL to call method.

您需要将正确的URL传递给调用方法。

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

#1


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.

您将在Getpath()方法中获取路径。

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 %> + "'}",
            })

#2


-1  

You Need to Pass correct URL to call method.

您需要将正确的URL传递给调用方法。

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