【问题标题】:how to call a asp.net webservice in android using phonegap如何使用phonegap在android中调用asp.net webservice
【发布时间】:2012-08-18 19:05:42
【问题描述】:

我有一个使用数据表从数据库访问值的 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;
    }

【问题讨论】:

  • mydbCon是service1.asmx中的方法名吗?可以显示 service1.asmx 的内容吗?
  • 我已经添加了我的 service1.asmx 看看
  • stackoverflow.com/a/2979938/169714 你为什么使用?wsdl
  • 我已经删除了 >wsdl 并检查了但没有工作
  • 因为你只是返回一个数据表而不是json,所以请使用json.codeplex.com

标签: javascript android jquery jquery-mobile cordova


【解决方案1】:

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

【讨论】:

  • 嘿,谢谢你的代码,但我无法在 Visual Studio Express 2010 中安装 json.net,因为我没有选项库包管理器。让我让我的管理员安装 VS ultimatum 并尝试任何方式非常感谢
  • 或者下载dll,放到你的bin文件夹里引用
  • 或者在visual web developer express stackoverflow.com/questions/4566908/…中打开你的解决方案你安装了nuget吗? visualstudiogallery.msdn.microsoft.com/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多