如何使用phonegap在android中调用asp.net Web服务

how to call a asp.net webservice in android using phonegap

本文关键字:asp net Web 服务 调用 何使用 phonegap android      更新时间:2023-09-26

我有一个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>  

我的服务1.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;
    }

添加Json。通过软件包管理器控制台或对话框将网络连接到您的解决方案

然后:

[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的链接。裸体画廊上的网络:http://nuget.org/packages/Newtonsoft.Json