为什么该代码会给出一个“;“未定义”;使用jquery.ajax从数据库获取标量值时出错?
你好,我是jquery.ajax中的begginer, 我试图从DB中获得一个标量值,但它没有定义,我不知道为什么。 下面是HTML页面为什么该代码会给出一个“;“未定义”;使用jquery.ajax从数据库获取标量值时出错?,jquery,asp.net,ajax,Jquery,Asp.net,Ajax,你好,我是jquery.ajax中的begginer, 我试图从DB中获得一个标量值,但它没有定义,我不知道为什么。 下面是HTML页面 <script src="jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#btn1').click(function () {
<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>
如果您试图通过AJAX从服务器接收项目,则需要将
type
设置为“GET”
而不是“POST”
,但我需要发送一个参数以通过web服务方法[x]
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;
}
}
}