2

我正在尝试为从 sitecore 创建的 json 文件中查找关键字的网站创建词汇表工具提示。我需要从 json 文件中获取“文本:”部分,然后在我的 jquery 中创建一个变量,以便它们是找到并用适当的标签包装的关键字。我让它工作到可以让控制台记录我的 json 文件中有 2 个条目的地步,仅此而已。

这是我的示例 json 代码:

[{"Id":"ef339eaa-78e1-4f9e-911e- 096a1920f0b6","Name":"Glossary","DisplayName":"Glossary","TemplateId":"b27d2588-3d02-4f5f-8064-2ee3b7b8eb39","TemplateName":"Glossary","Url":"/Global-Content/Glossary/Glossary","Version":1,"Created":"\/Date(1343987220000)\/","CreatedBy":"sitecore\\rgoodman","Revision":"ae8b3ae0-d0ca-4c4a-9f27-a542a31ab233","Updated":"\/Date(1348137810133)\/","UpdatedBy":"sitecore\\admin","Text":"Glossary","Content":"A bit of test content for the glossary"},{"Id":"3fa51ad4-cfb6-4ff1-a9b5-5276914b2c23","Name":"Abraham","DisplayName":"Abraham","TemplateId":"b27d2588-3d02-4f5f-8064-2ee3b7b8eb39","TemplateName":"Glossary","Url":"/Global-Content/Glossary/A/Abraham","Version":1,"Created":"\/Date(1348148640000)\/","CreatedBy":"sitecore\\admin","Revision":"231284ec-9fb9-4502-ad79-a5806479ecba","Updated":"\/Date(1348148779656)\/","UpdatedBy":"sitecore\\admin","Text":"Abraham","Content":"This is a lincoln person"}]

但我想这没有任何用处,因为它只是我想要返回的“文本:”部分。

这是我的jQuery:

function getData(url) {
var data;
    $.ajax({
        async: false,
        url: '/_assets/js/glossary.json',
        dataType: 'json',
        success: function(data.Text){
           data.Text = response;
        }
        return(response);
    });
}


function HighlightKeywords(keywords)
{         
var el = $("body");
$(keywords).each(function()
{
    var pattern = new RegExp("(" +this+ ")", ["gi"]);
    var rs = "<mark href='#' class='tooltip'>$1</mark>";
    el.html(el.html().replace(pattern, rs));
});
}        

HighlightKeywords(data.Text);

本质上,我需要返回数据位于 HighlightKerywords 函数上的 json 的“文本:”位。我哪里错了?

任何帮助将非常感激。谢谢

4

2 回答 2

3

您的函数的语法格式不正确。您的返回必须进入同步示例中的成功函数内部,而不是随机放置在 ajax 对象中。

function getData() {
    $.ajax({
        async: false,
        url: '/_assets/js/glossary.json',
        dataType: 'json',
        success: function(data){
           //HighlightKeywords(data.Text);
           //or
           return(data.Text);
        }

    });
}
于 2012-09-24T13:00:55.263 回答
1

Ajax 是异步通信,您不能将其响应插入到全局变量中并期望能够使用它。

您需要在 success 函数中对 data.text 进行所有工作。

success: function(response){
           HighlightKeywords(response.Text);
        }
于 2012-09-24T13:01:10.080 回答