如何使用ajax.jquery从DB获取sclar [英] how to get a sclar from DB using ajax.jquery
本文介绍了如何使用ajax.jquery从DB获取sclar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hiiii,我是jquery.ajax中的begginer,我尝试从DB获取标量值,但它提示未定义,我不知道为什么。下面是HTML页面
hiiii , i am begginer in jquery.ajax , i try to get a scalar value from DB but it alert undefined , i don't know why . below is HTML page
<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>
以下是.cs页面
below is .cs page
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;
}
}
}
推荐答案
(文档)。ready( function (){
(document).ready(function () {
(' #btn1')。click( function (){
('#btn1').click(function () {
>
.ajax({
type: POST,
url: WebForm1.aspx / getdep,
data:' {x:1}',
datatype: json,
contentType: application / json charset-utf-8,
成功: function (data){
alert(data.d);
},
});
});
});
< / script >
.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>
以下是.cs页面
below is .cs page
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;
}
}
}
这篇关于如何使用ajax.jquery从DB获取sclar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文