我正在尝试使用phonegap开发一个简单的应用程序访问所有联系人。但每次我尝试运行代码时,都会调用onError()函数而不是onSuccess()。
以下是代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
var options = new ContactFindOptions();
options.filter = "";
options.multiple=true;
var fields = ["displayName"];
navigator.contacts.find(fields, onSuccess, onError, options);
}
var names=[];
function onSuccess(contacts)
{
for (var i = 0; i < contacts.length; i++)
{
names.push("Display Name = " + contacts[i].displayName);
}
alert(names);
}
function onError(contactError)
{
alert('error !!');
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
</div>
<div data-role="content">
<p>
Hello Phonegap!!
</p>
</div>
<div data-role="footer">
<h1>Footer Text</h1>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = new ContactFindOptions();
options.filter="";
options.filter="";
options.multiple=true;
var fields = ["*"]; //"*" will return all contact fields
navigator.contacts.find(fields, onSuccess, onError, options);
}
// display the address information for all contacts
function onSuccess(contacts) {
//console.log(JSON.stringify(contacts))
var li = '<ol data-inset="true" data-role="listview">';
$.each(contacts, function(key, value) {
if(value.name){
$.each(value.name, function(key, value) {
if(key == 'formatted'){
name = value;
}
});
}
if(value.phoneNumbers){
$.each(value.phoneNumbers, function(key, value) {
phone = value.value;
});
}
li += '<li style="text-decoration:none;">'+name+' '+phone+'</li>';
});
li += '</ol>';
$("#contact").html(li).trigger('create');
$("#contact").listview("refresh");
};
function onError(contactError) {
alert('onError!');
};
</script>