简体   繁体   English

传递给SCEditor时无法使JSON对象正常工作

[英]Having trouble getting JSON object to work when passed to SCEditor

I am using SCEditor and I am trying to set my own custom emoticons according to the emoticons option specified on this page . 我正在使用SCEditor,并且尝试根据此页面上指定的表情符号选项设置自己的自定义表情符号。

So I called it like so: 所以我这样称呼它:

$(document).ready(function() {

    $(".sceditor").sceditor({
        // Other options
        emoticons: $.getJSON('../../images/emoticons/default/emoticons.json'),
        emoticonsRoot: '../../images/emoticons/default/'
    });

})

Then in my emoticons.json file I have this: 然后在我的emoticons.json文件中,我有这个:

{
    dropdown: {
        ':)': 'emoticons/smile.png',
        ':angel:': 'emoticons/angel.png',
        ':angry:': 'emoticons/angry.png'
    }
}

However it is not working. 但是,它不起作用。 I have checked the NET panel in my browser and I have confirmed it is fetching the .json file fine, however when I click to open the smiley window in the editor it is blank (all I see is the "more" link). 我已经在浏览器中检查了NET面板,并确认它可以正常获取.json文件,但是,当我单击以在编辑器中打开笑脸窗口时,它是空白的(我所看到的只是“更多”链接)。

Am I doing something wrong here? 我在这里做错什么了吗?

Json lint show above text in json file is invalid. JSON文件中的json文件上面的文本无效。

Try putting this in your json file 尝试将其放在您的json文件中

{
    "dropdown": {
        ":)": "emoticons/smile.png",
        ":angel:": "emoticons/angel.png",
        ":angry:": "emoticons/angry.png"
    }
}

I tried this in php 我在php中尝试过

$a = '{
    "dropdown": {
        ":)": "emoticons/smile.png",
        ":angel:": "emoticons/angel.png",
        ":angry:": "emoticons/angry.png"
    }
}' ;
print_r(json_decode($a));

Output 产量

Array
(
    [dropdown] => Array
        (
            [:)] => emoticons/smile.png
            [:angel:] => emoticons/angel.png
            [:angry:] => emoticons/angry.png
        )

)

I have tried by your link. 我已尝试通过您的链接。 The follows is my code: 以下是我的代码:

$(function() {
        // Replace all textarea's
        // with SCEditor
        $("textarea").sceditor({
            plugins: "bbcode",
            style: "plugins/SCEditor/development/jquery.sceditor.default.css",
            emoticons: {
                // Emoticons to be included in the dropdown
                dropdown: {
                    ":)": "emoticons/smile.png",
                    ":angel:": "emoticons/angel.png"
                },
                // Emoticons to be included in the more section
                more: {
                    ":alien:": "emoticons/alien.png",
                    ":blink:": "emoticons/blink.png"
                },
                // Emoticons that are not shown in the dropdown but will still
                // be converted. Can be used for things like aliases
                hidden: {
                    ":aliasforalien:": "emoticons/alien.png",
                    ":aliasforblink:": "emoticons/blink.png"
                }
            },
            emoticonsRoot: "plugins/SCEditor/"
        });
    });

Above this, you may kown why your code is wrong. 在此之上,您可能知道为什么您的代码错误。 Accross from jQuery.getJSON , $.getJSON returns JQXHR,not a Json object. jQuery.getJSON到 $ .getJSON返回JQXHR,而不是Json对象。 So, It is not possible to show your emoticons. 因此,不可能显示您的图释。

As per WhiteWater's answer the reason it wasn't working was because it was returning a JQXHR object and not a JSON object, so I had to work out how to get it to return a JSON object whilst the editor loading not being dependent on the emoticons existence. 根据WhiteWater的回答 ,它不起作用的原因是因为它返回的是JQXHR对象而不是JSON对象,所以我必须弄清楚如何使它返回JSON对象,同时编辑器加载不依赖于表情存在。

So we have the solution below with credit to jeremysawesome for his answer to this question : 因此,我们有以下的信用解决jeremysawesome回答这个问题

// create a variable to store the results
var emoticons = false;

$.getJSON('../../images/emoticons/default/emoticons.json')
.done(function(result){
    emoticons = result;
})
.always(function(){
    // always initialize the sceditor
    $('.my-selector').sceditor({emoticons: emoticons}); 
});

This will load the emoticons but will always load the sceditor instance, even if the emoticons fail for some reason. 这将加载表情,但始终会加载sceditor实例,即使表情由于某种原因失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM