我正在使用Ajax for Bootstrap Modal从数据库中提取内容。 我的函数文件中有一个函数,用于检查是否存在远程文件:
function checkRemoteFile($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec($ch) !== FALSE) {
return true;
} else {
return false;
}
}
我试图在我的ajax中调用上面的函数,这是:
$(document).on('click', '.show-detail', function (e) {
e.preventDefault();
$('#dynamic-content').html('');
$('#modal-loader').show();
$('#view-modal').modal();
$.ajax({
url: '',
type: 'post',
data: {GetData: 1, EmployeeID: $(this).data('id')},
dataType: 'json'
}).done(function (abc) {
var number = '';
$.each(abc[0].Phone, function (i, item) {
number += '<p><span class="icon_phone" data-toggle="tooltip" data-placement="left" title="Phone/Mobile" /></span> ' + item.Phone + '' + (item.Extension !== "" ? ' Ext#: ' + item.Extension : '') + '</p>';
});
var email = '';
$.each(abc[0].Email, function (i, item) {
email += '<p><span class="icon_mail" data-toggle="tooltip" data-placement="left" title="Phone/Mobile" /></span> <a href="mailto:' + item.Email + '">' + item.Email + '</a></p>';
});
var pic ='';
$.each(abc, function (i, item) {
if (checkRemoteFile('http://http://url.com/img/' + item.pictureCode+'.jpg?code=xxxx')){
pic += '<img src="http://http://url.com/img/'+ item.pictureCode+'.jpg?code=xxxx" width= "50px" height= "50px" />';
} else {
pic += '<img src="assets/img/avatars/unknown-user.png" width= "50px" height= "50px" />';
}
});
var temp = '<div class="row">\n\
<div class="col-md-6">\n\
<p> '+ pic + '\n\
<h4>' + abc[0].FullName + '</h4>\n\
' + number + '\n\
' + email + '\n\
</div>\n\
</div>';
$('#dynamic-content').html(temp);
$('#modal-loader').hide();
}).fail(function () {
$('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
$('#modal-loader').hide();
});
});
我正在尝试使用以下方法调用该函数:
var pic ='';
$.each(abc, function (i, item) {
if (checkRemoteFile('http://url.com/img/' + item.pictureCode+'.jpg?code=xxxx')){
pic += '<img src="http://url.com/img/'+ item.pictureCode+'.jpg?code=xxxx" width= "50px" height= "50px" />';
} else {
pic += '<img src="assets/img/avatars/unknown-user.png" width= "50px" height= "50px" />';
}
});
但它不起作用