2012-03-31 46 views
1

我正在使用PhoneGap開發Windows Phone 7應用程序。我試圖調用一個webservice作爲JSON字符串返回一個Child對象列表(這是一個社會工作者的研究項目)。這是我到目前爲止有:使用jQuery Mobile訪問Web服務

 <!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> 

    <title>Login Page</title> 

     <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8"/> 

     <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script> 
     <script type="text/javascript"" src="../Scripts/jquery.mobile-1.0.1.js"></script> 

     <script type="text/javascript"> 

      document.addEventListener("deviceready", onDeviceReady, false); 

      // once the device ready event fires, you can safely do your thing! -jm 
      function onDeviceReady() { 

      } 

      function LoginButton_onclick() { 
      var email=document.getElementById("EmailBox").value; 
      var pass=document.getElementById("PasswordBox").value; 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren", 
       data: '{ "email" : "' + email + '", "password": "' + pass + '" }', 
       dataType: "json", 
       success: GetChildrenSuccess, 
       failure: GetChildrenFailed 
      }); 
     } 

     function GetChildrenSuccess(response) { 
      var children = eval('(' + response.d + ')'); 
      var child; 
      for(child in children) { 
       document.getElementById('ResultsDiv').innerHTML = "ID: "+child.ID+ " Name: "+child.Name+" Surname: "+child.Surname+" \r\n"; 
      } 
     } 

     function GetChildrenFailed(error) { 
      document.getElementById('ResultsDiv').innerHTML = "Error"; 
     } 
     </script> 

    </head> 
    <body> 
    <h1>Please Login:</h1> 

    <div id="LoginDiv"> 
     Email: <input id="EmailBox" type="text" /><br /> 
     Password: <input id="PasswordBox" type="password" /><br /> 

     <input id="LoginButton" type="button" value="Submit" onclick="LoginButton_onclick()" /> 
    </div> 
    <div id="ResultsDiv"> 
    </div> 
    </body> 
</html> 

Web服務:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class GetChildren : System.Web.Services.WebService { 

public GetChildren() { 

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
} 

MD5 md5Hash = MD5.Create(); 
    string conString = ConfigurationManager.ConnectionStrings["SponsorChildDatabase"].ConnectionString; 

    [WebMethod(Description = "Returns the list of children for whom the social worker is responsible.")] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public String GetMyChildren(String email, String password) 
    { 
     DataSet MyChildren = new DataSet(); 

     int ID = SocialWorkerLogin(email, password); 
     if (ID > 0) 
     { 
      MyChildren = FillChildrenTable(ID); 
     } 
     MyChildren.DataSetName = "My Children"; //To prevent 'DataTable name not set' error 

     List<Child> children = new List<Child>(); 
     foreach (DataRow rs in MyChildren.Tables[0].Rows) 
     { 
      Child c=new Child(rs["Child_ID"].ToString(), rs["Child_Name"].ToString(), rs["Child_Surname"].ToString()); 
      children.Add(c); 
     } 

     // Return JSON data 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string strJSON = js.Serialize(children); 
     return strJSON; 
    } 

子類:

public class Child 
{ 
    String id; 

    public String ID 
    { 
     get { return id; } 
     set { id = value; } 
    } 
    String name; 

    public String Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 
    String surname; 

    public String Surname 
    { 
     get { return surname; } 
     set { surname = value; } 
    } 

    public Child(String Cid, String Cname, String Csurname) 
    { 
     this.ID = Cid; 
     this.Name = Cname; 
     this.Surname = Csurname; 
    } 
} 

當我測試自身的網絡服務(即進入在我的常規瀏覽器中的url)它的工作原理,但按下提交按鈕不會在我的移動應用程序中做任何事情。

這是我的第一個移動應用程序,所以我甚至不知道如何正確調試它,並且不知道是什麼問題,因爲它似乎什麼都不做。我想也許Web服務需要託管在IIS中(也沒有這方面的經驗),但因爲它允許我添加服務引用它似乎找到它。任何想法問題可能是/如果我的方法是正確的?

+0

我沒有看到你把你的點擊事件連接到按鈕。你有一個處理程序,但是,根據你顯示的代碼,它沒有連接 – Alex 2012-03-31 11:03:37

+0

好的,添加了其餘的html,是不是你如何鏈接到一個JavaScript函數的按鈕? – Matt 2012-03-31 11:08:50

回答

2

我看到一些我想提請注意的問題。

  1. ,因爲你已經在使用jQuery,$( '#身份證')不使用的getElementById,要好得多。

  2. 對於在$ .ajax方法中發佈數據,使用JSON.stringify(data)來代替通過自己構造字符串對象會好得多。

  3. 你在callback函數中做了eval結果,這是錯誤的。 jQuery爲你做初始評估。您必須這樣做,因爲您使用的是Web服務錯誤。切勿通過yourseft進行json序列化,也不要返回字符串。反過來,ASMX會自動爲你做序列化。

不幸的是,您的問題並不具體,所以很難給出答案。由於您的應用程序是Web移動應用程序,因此您可以在瀏覽器中啓動它。使用FireBug或Chrome開發工具來調試你的JS,看看有什麼不對。使用Visual Studio來調試您的服務代碼。

+0

謝謝,爲我清除了很多東西,並至少能夠找到問題。 – Matt 2012-03-31 14:39:38

+0

@Matt,你可以發佈你的解決方案,以便其他人有同樣的問題可以看到你做了什麼? – jlafay 2012-07-19 21:24:02

+0

嗨亞歷山大..我會問一個關於你的答案的問題!在你的答案3你說var children = eval('('+ response.d +')');是錯誤的,jQuery爲你做初始評估!這是什麼意思? – Mehmet 2012-08-23 11:20:30