在Sharepoint在线页面中,Jquery休息称为Image not dispalyed

时间:2018-04-06 11:52:31

标签: sharepoint-online

当我成功获取图片库列表项时,我正在开发一个共享点托管在线应用程序。但是图像不会显示在页面中。在这里,我附上我的代码是错的吗?可以任何人指导我。我是Share point online App的新手。



function LoadImages()  {
       appWebUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;
       hostWebUrl = _spPageContextInfo.siteAbsoluteUrl;
     $(function () {
         var utilTargetsList = "CustomerLicenceGallary";
        $.ajax({
            url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + utilTargetsList + "')/items?@target='" + hostWebUrl + "'",
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
            },
            success: function (d) {
                var stringData = JSON.stringify(d);
                var jsonObject = JSON.parse(stringData);
                var results = jsonObject.d.results;
                for (var i = 0; i < results.length; i++) {
                    jQuery('#Image').attr('src', results[i]["Name"]);
                    break;
                }
            },
            //success: getitemssucces
            error: function (error) {
                console.log(JSON.stringify(error.toString()));
            }
        });
    });
}
  
$(document).ready(function () {
    LoadImages();
});
&#13;
<div>  <img id="Image" alt="Image" /></div>
&#13;
&#13;
&#13;

PictureLibrary List

1 个答案:

答案 0 :(得分:0)

我得到了答案并且工作得非常感谢所有人的支持。

&#13;
&#13;
var arrayOfImageObjects = new Array();
var currentImageIndex = 0;
var hostWebUrl;
var appWebUrl;

function GetImagesAndRotate() {

    appWebUrl = window.location.protocol + "//" + window.location.host
             + _spPageContextInfo.webServerRelativeUrl;
    hostWebUrl = _spPageContextInfo.siteAbsoluteUrl;

   // var url = appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('CustomerLicenceLibrary')/items?$expand=File"
   //url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + utilTargetsList + "')/items?$top=1&@target='" + hostWebUrl + "'&$Orderby=ID desc",

    jQuery.ajax({
        url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('CustomerLicenceLibrary')/items?$expand=File&@target='" + hostWebUrl + "'",
        type: "GET",
        headers: {
            "X-HTTP-Method": "MERGE",
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        },
        success: function (data) {
            var result = data.d.results;
            for (var i = 0; i < result.length; i++) {
                var image = result[i];
                var imageObject = {};
                imageObject.Name = image.File.Name;
                imageObject.Url = image.File.ServerRelativeUrl;
                imageObject.Width = image.File.ImageWidth;
                imageObject.Height = image.File.ImageHeight;
                arrayOfImageObjects.push(imageObject);
            }
            setTimeout(NextImage, 5000);
        },
        error: function () {
            alert("request failed");
        }
    });
}

function NextImage() {
    if (currentImageIndex < arrayOfImageObjects.length) {
        jQuery('#Image').attr('src', arrayOfImageObjects[currentImageIndex].Url);
        currentImageIndex++;
    }
    else
        currentImageIndex = 0;
    setTimeout(NextImage, 5000);
}

jQuery(document).ready(function () {
    GetImagesAndRotate();
});
&#13;
&#13;
&#13;