hiiii,我是jquery.ajax的初学者, 我试图从数据库中获取标量值,但它警告未定义,我不知道为什么。 下面是HTML页面
<script src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btn1').click(function () {
$.ajax({
type: "POST",
url: "WebForm1.aspx/getdep",
data: '{x:1}',
datatype: "json",
contentType: "application/json charset-utf-8",
success: function (data) {
alert(data.d);
},
});
});
});
</script>
以及code.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[webmethod]
public string getdep(int x)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select depname from dep where depid="+x, connection);
connection.Open();
string result = cmd.ExecuteScalar().ToString();
return result;
}
}
}