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

时间:2012-08-23 09:31:04

标签: javascript android jquery jquery-mobile cordova

我有一个asp.net Web服务使用数据表从数据库访问值 我的javascript在eclipse中就像这样,它在android模拟器中使用phonegap运行,但这段代码似乎无法正常工作.pls帮助我。

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

1 个答案:

答案 0 :(得分:3)

使用package manager consoledialog

将Json.Net添加到您的解决方案中

然后:

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

以下是nuget图库中Json.Net的链接:http://nuget.org/packages/Newtonsoft.Json

相关问题