Я использую PhoneGap для разработки приложения для Windows Phone 7. Я пытаюсь вызвать веб-сервис, который возвращает объекты List of Child (это исследовательский проект для социальных работников) в виде строки JSON. Это то, что у меня так далеко:
<!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>
Веб-сервис:
[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 в моем обычном браузере), он работает, но нажатие кнопки отправки ничего не делает в моем мобильном приложении.
Это мое первое мобильное приложение, поэтому я даже не знаю, как правильно его отладить, и понятия не имею, в чем проблема, поскольку оно, похоже, ничего не делает. Я подумал, что, возможно, веб-сервис должен быть размещен в IIS (также нет опыта работы с ним), но, поскольку он позволяет мне добавить ссылку на сервис, кажется, он ее нашел. Любые идеи, в чем может быть проблема / если мой подход даже правильный?