我有使用数据表中的asp.net web服务,从访问数据库值和我的JavaScript是这样在它采用的是Android模拟器使用PhoneGap的运行Eclipse但这code似乎并没有被工作.PLS帮助我。
<脚本类型=文/ JavaScript的> 功能GetAge(){ jQuery.support.cors = TRUE; $ .mobile.allowCrossDomainPages = TRUE; $阿贾克斯({ 数据:DATAS, 键入:POST, 异步:假的, 数据类型:JSON 的contentType:应用/ JSON的;字符集= UTF-8, 网址:HTTP://本地主机:50113 / Service1.asmx的/ mydbCon WSDL 成功:函数(MSG){ $('#divToBeWorkedOn')HTML(msg.text)。 }, 错误:功能(E){ $('#divToBeWorkedOn')的html(不可用)。 } }); } < / SCRIPT>
和我Service1.asmx的是这样
[的WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json) 公开数据表mydbCon() { SqlConnection的SqlCon =新的SqlConnection(); SqlCon.Open(); 的SqlCommand SqlComm =新的SqlCommand(); SqlComm.Connection = SqlCon; SqlComm.CommandType = CommandType.Text; SqlComm.CommandText =从tbl_login选择密码其中username ='奥比';; 数据表EmployeeDt =新的DataTable(tbl_login); SqlDataAdapter的SQLDA =新SqlDataAdapter的(SqlComm); SqlDa.Fill(EmployeeDt); 返回EmployeeDt; }
解决方案
添加Json.Net与的包管理器控制台或通过的对话框
和则:
[的WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)公共字符串mydbCon(){ SqlConnection的SqlCon =新的SqlConnection(); SqlCon.Open(); 的SqlCommand SqlComm =新的SqlCommand(); SqlComm.Connection = SqlCon; SqlComm.CommandType = CommandType.Text; SqlComm.CommandText =从tbl_login选择密码其中username ='奥比';; 数据表EmployeeDt =新的DataTable(tbl_login); SqlDataAdapter的SQLDA =新SqlDataAdapter的(SqlComm); SqlDa.Fill(EmployeeDt); 返回JsonConvert.SerializeObject(EmployeeDt,Formatting.Indented);}
下面是Json.Net上的NuGet库的链接: http://nuget.org/packages /Newtonsoft.Json
I have a asp.net web service accessing value from database using a datatable and my javascript goes like this in eclipse where it is running in android simulator using phonegap but this code seems not to be working .pls help me out.
<script type="text/javascript">
function GetAge() {
jQuery.support.cors = true;
$.mobile.allowCrossDomainPages = true;
$.ajax({
data: datas,
type: "POST",
async: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://localhost:50113/Service1.asmx/mydbCon?wsdl",
success: function (msg) {
$('#divToBeWorkedOn').html(msg.text);
},
error: function (e) {
$('#divToBeWorkedOn').html("unavailable");
}
});
}
</script>
and my service1.asmx goes like this
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DataTable mydbCon()
{
SqlConnection SqlCon = new SqlConnection("");
SqlCon.Open();
SqlCommand SqlComm = new SqlCommand();
SqlComm.Connection = SqlCon;
SqlComm.CommandType = CommandType.Text;
SqlComm.CommandText = "select password from tbl_login where username='aby';";
DataTable EmployeeDt = new DataTable("tbl_login");
SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
SqlDa.Fill(EmployeeDt);
return EmployeeDt;
}
解决方案
Add Json.Net to your solution with the package manager console or by the dialog
and then:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string mydbCon()
{
SqlConnection SqlCon = new SqlConnection("");
SqlCon.Open();
SqlCommand SqlComm = new SqlCommand();
SqlComm.Connection = SqlCon;
SqlComm.CommandType = CommandType.Text;
SqlComm.CommandText = "select password from tbl_login where username='aby';";
DataTable EmployeeDt = new DataTable("tbl_login");
SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
SqlDa.Fill(EmployeeDt);
return JsonConvert.SerializeObject(EmployeeDt, Formatting.Indented);
}
Here is the link of Json.Net on the nuget gallery: http://nuget.org/packages/Newtonsoft.Json