gpt4 book ai didi

c# - 想要使用 jQuery AJAX 将文本字段值添加到数据库

转载 作者:行者123 更新时间:2023-11-28 11:03:49 25 4
gpt4 key购买 nike

我想使用 javascript jQuery AJAX 调用 C# 服务器方法将文本字段值插入数据库。问题是 AJAX 调用成功运行,但 C# 没有更新数据库。可能是什么原因?请尽快回复。ASP代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dbTest.aspx.cs" Inherits="dbTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<title></title>
<script src="jquery.js" type="text/javascript"></script>
<script src="json2.js" type="text/javascript"></script>

<script type="text/javascript">

function SaveOnClick() {
$.ajax({
type: "POST",
url: "dbTest.aspx/SaveData",
data: "{'CustomerName': '" + $('#txtCustomerName').val() + "', 'CustomerPhoneNumber': '" + $('#txtCustomerPhoneNumber').val() + "', 'ColorID': '" + $('#txtColorID').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result, txtstatus) {
alert(txtstatus);
},
error: function(result) {
alert("jQuery Error:" + result.statusText);
}
});
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="Customers" runat="server">
</asp:DropDownList>
</div>
<div id="CustomerDetails">
</div>
<table border="0">
<tr>
<td align="right"><strong>Customer Name:</strong></td>
<td align="left">
<asp:TextBox id="txtCustomerName" maxlength="50" runat="server" />
</td>
</tr>
<tr>
<td align="right"><strong>Customer Phone Number:</strong></td>
<td align="left">
<asp:TextBox id="txtCustomerPhoneNumber" runat="server" />
</td>
</tr>
<tr>
<td align="right"><strong>Color ID:</strong></td>
<td align="left">
<asp:TextBox id="txtColorId" runat="server" />
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>

<input type="button" onclick="SaveOnClick()" value="click me"/>

<asp:Panel id="pnlConfirm" runat="server">
<h2>Thanks for submitting your information to us!</h2>

</asp:Panel>

</form>
</body>
</html>

下面是 C# 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Services;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class dbTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
pnlConfirm.Visible = false;
}
[WebMethod]
public static void SaveData(string CustomerName, string CustomerPhoneNumber, string ColorID)
{
string Server = "al2c06";
string Username = "app_Testing";
string Password = "jsjsdj";
string Database = "AmbreenTest";

string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
string query = "INSERT INTO Customer_Order(customerName, customerPhoneNumber, colorID)";
query += "VALUES (";
query += "'" + CustomerName+ "'";
query += ",";
query += "'" + CustomerPhoneNumber + "'";
query += ",";
query += "'" + ColorID+ "'";
query += ")";

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
}
}
//lblSQL.Text = query;
//pnlConfirm.Visible = true;
}
}

最佳答案

首先修复您的 jQuery 选择器,它们区分大小写。将 $('#txtColorID').val() 更改为 $('#txtColorId').val()

关于c# - 想要使用 jQuery AJAX 将文本字段值添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com