gpt4 book ai didi

javascript - 如何使用phonegap在android中调用asp.net webservice

转载 作者:行者123 更新时间:2023-11-28 20:52:56 25 4
gpt4 key购买 nike

我有一个 ASP.NET Web 服务,使用数据表从数据库访问值我的javascript在eclipse中是这样的,它使用phonegap在android模拟器中运行,但这段代码似乎不起作用。请帮助我。

<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>

我的 service1.asmx 是这样的

[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;
}

最佳答案

使用 package manager console 将 Json.Net 添加到您的解决方案中或通过 dialog

然后:

[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);
}

这是 Json.Net 在 nuget 画廊上的链接:http://nuget.org/packages/Newtonsoft.Json

关于javascript - 如何使用phonegap在android中调用asp.net webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12088693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com