Reputation: 297
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;
}
Upvotes: 0
Views: 2049
Reputation: 6047
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
Upvotes: 3