2012-08-23 39 views
0

我有一個asp.net web服務從數據庫訪問值使用數據表 和我的JavaScript在eclipse中是這樣的,它使用phonegap在android模擬器中運行,但是這個代碼似乎不工作.pls幫助我。如何在android中使用phonegap調用asp.net webservice

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

是mydbCon service1.asmx中的方法名稱?你可以顯示service1.asmx的內容嗎? –

+0

我已經添加了我的service1.asmx檢查出來 – dennis

+0

http://stackoverflow.com/a/2979938/169714以及爲什麼使用'?wsdl'? –

回答

3

Json.Net添加到與package manager console您的解決方案或由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); 
} 

以下是關於nuget gallery的Json.Net鏈接:http://nuget.org/packages/Newtonsoft.Json

+0

嘿感謝代碼,但我不能insdtall json.net在Visual Studio Express 2010,因爲我沒有選項庫包管理器。讓我問我的管理員安裝VS最後通and,並試試這個出來的任何方式感謝很多 – dennis

+0

或下載DLL,把它放在你的bin文件夾,並引用它 –

+0

或打開您的解決方案在可視化Web開發人員表達http://stackoverflow.com/questions/4566908/how-can-i-use-nuget-with-visual-c-sharp -express你有沒有安裝nuget? http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c –

相關問題