0

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

1 回答 1

3

使用包管理器控制台对话框将 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

于 2012-08-23T10:02:42.977 回答