如果更改页面上的Javascript字符串链接不起作用,我不知道为什么?

时间:2017-07-10 11:08:57

标签: javascript json sharepoint

我有一个查询SharePoint列表的JavaScript文件。它工作得很好,但返回了一条“未定义”的行,我无法解决:

enter image description here

很好,所以我想我会把它从代码中删除。

如果我使用此字符串:

      txtTitle = txtTitle + "<p><a  href='/sites/it/ITInfrastructure/SitePages/Service%20Catalog‌​ue%20Devices.aspx?di‌​d=" + tempID + "'>" + tempTitle + "</a>" + "</p>";

未定义消失但是没有链接可以工作,尽管如果我使用这个显示'undefined'的字符串是完全相同的链接?!

txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=" + tempID + "'>" + tempTitle +  "</a> - " + tempDeviceType + "</p>";

这是一个链接示例:http://example.com/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=10

我正在使用的两个js文件的代码(getDevices和getDeviceDetails)如下:

getDevices

function getDevices() {
var txtTitle = "";
var txtDeviceType = "";


   var query = "http://example.com/sites/it/ITInfrastructure/_vti_bin/listdata.svc/Devices?select=ID,Title";

var call = $.ajax({
        url: query,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }       
    });


call.done(function (data,textStatus, jqXHR){
    $.each(data.d.results, function (i, result) {

        var tempID = result.Id;
        var tempTitle = result.Title;
        var tempDeviceType = result.DeviceTypeValue;



        txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=" + tempID + "'>" + tempTitle +  "</a> - " + tempDeviceType + "</p>";
        //txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalog‌​ue%20Devices.aspx?di‌​d=" + tempID + "'>" + tempTitle + "</a>" + "</p>";



    });
    $('#devices').append($(txtTitle));
});
call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Error retrieving data: " + jqXHR.responseText);
});
}

getDeviceDetails

function getDeviceDetails() {
var txtTitle = "";
var txtName = "";
var txtOverview = "";
var txtAccessories = "";
var txtDevicetype = "";
var txtTypicalDeviceUsage ="";
var txtKnownSystemIssues ="";
var txtTrafficlight = "";
var imgDevicePicture = "";
var tempLCS2 = "";
var HTML = "<h3>Desktop</h3>";
var Laptop = "Y"
var Tabs="Y" ;  
  var query  
  = "http://example.com/sites/it/ITInfrastructure/_vti_bin/listdata.svc/Devices?$expand=Priority&$filter=Id eq " + window.DeviceId + "";




var call = $.ajax({
        url: query,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }       
    });
call.done(function (data,textStatus, jqXHR){
$.each(data.d.results, function(index, item) {
        var tempID = item.Id;
        var NameofDevice = item.Title;
        var tempTitle = item.Title;
        var DeviceOverView = item.Description;
        var AccessDetails = item.Accessories;
        var DeviceKind = item.DevicetypeValue;
        var Usage = item.TypicalUsage;
        var DevicePriority = item.PriorityValue;
        var DeviceImage = item.DeviceImage;

        txtTitle = "<p>";  

        txtName = "<p>" + NameofDevice + "</p>" +  DeviceKind + "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
        txtAccessories = "<p>" + AccessDetails + "</p>";  
        txtDevicetype = "<p>" + DeviceKind  + "</p>";
        txtTypicalDeviceUsage = "<p>" + Usage + "</p>";
        txtTrafficlight = "<p>" + DevicePriority + "</p>";
        imgDevicePicture = "<img src='" + DeviceImage + "'>";

    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
    $('devicename').append(txtName);
    $('#devicekind').append(txtDevicetype);
    $('#deviceacc').append(txtAccessories);
    $('#deviceuse').append(txtTypicalDeviceUsage);
    $('#devicestatus').append(txtTrafficlight);
    $('#imageContainer').append("<img src='/sites/IT/ITInfrastructure/SiteAssets/"+txtTrafficlight.replace(/<[^>]*>/g, '')+".png' />");
    $('.deviceimage').append(imgDevicePicture); 

});
call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Error retrieving data: " + jqXHR.responseText);
});

}

2 个答案:

答案 0 :(得分:0)

你有一些charset问题。当我复制粘贴行:

    txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=" + tempID + "'>" + tempTitle +  "</a> - " + tempDeviceType + "</p>";
    //txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalog‌​ue%20Devices.aspx?di‌​d=" + tempID + "'>" + tempTitle + "</a>" + "</p>";

在使用ANSI编码的notepadd ++中,我得到:

txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=" + tempID + "'>" + tempTitle +  "</a> - " + tempDeviceType + "</p>";
//txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalog??ue%20Devices.aspx?di??d=" + tempID + "'>" + tempTitle + "</a>" + "</p>";

注意&#34; ??&#34;在&#34;服务%20Catalogue%20Devices&#34;和&#34;做了#34;部分。我认为这是导致链接失败的原因。

答案 1 :(得分:0)

这有助于淘汰未定义的&#39;:

txtTitle = txtTitle + "<p><a href='/sites/it/ITInfrastructure/SitePages/Service%20Catalogue%20Devices.aspx?did=" + tempID + "'>" + tempTitle + "</a></p>";