我正在使用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中(也沒有這方面的經驗),但因爲它允許我添加服務引用它似乎找到它。任何想法問題可能是/如果我的方法是正確的?
我沒有看到你把你的點擊事件連接到按鈕。你有一個處理程序,但是,根據你顯示的代碼,它沒有連接 – Alex 2012-03-31 11:03:37
好的,添加了其餘的html,是不是你如何鏈接到一個JavaScript函數的按鈕? – Matt 2012-03-31 11:08:50