【问题标题】:AJAX Javascript Strings and PyamidAJAX Javascript 字符串和金字塔
【发布时间】:2013-11-11 22:01:27
【问题描述】:

我正在构建一个金字塔网络应用程序,并且我正在尝试实现 JavaScript 降价编辑器 EpicEditor 以编辑降价文件。

$.ajax({
    url: "{{ request.resource_url(context) }}raw_markdown",
    context: document.body,
    success: function(md){
        markdown = md;
    }
})
var opts = { 
    basePath: '{{ request.static_url('plcars:static/') }}',
    focusOnLoad: true,
    clientSideStorage: false,
    autogrow: true,
    file: { defaultContent: markdown }
};
var editor = new EpicEditor(opts);
editor.load();

但 EpicEditor 的行为就像 markdown 为空一样。如果我调用alert(markdown),它也是空的,如果我尝试document.write(markdown);,也没有任何反应。

我知道 ajax 调用的 URL 有效,正如我在 Firefox Web 控制台中看到的那样,请求成功。此外,如果我在控制台中键入以查看变量 markdown 的值,它应该是正确的(例如,"This is my text")。

在此之前,我曾尝试通过 JSON 容器传递 markdown,但似乎进展顺利,只是该字符串永远无法显示在页面上。

【问题讨论】:

    标签: javascript ajax pyramid


    【解决方案1】:

    你应该把你的代码放在success回调中,因为你的$.ajax调用是异步的:

    $.ajax({
        url: "{{ request.resource_url(context) }}raw_markdown",
        context: document.body,
        success: function (md) {
            markdown = md;
            var opts = {
                basePath: '{{ request.static_url('plcars:static/') }}',
                focusOnLoad: true,
                clientSideStorage: false,
                autogrow: true,
                file: {
                    defaultContent: markdown
                }
            };
            var editor = new EpicEditor(opts);
            editor.load();
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2021-07-17
      • 1970-01-01
      • 2014-07-02
      • 2019-09-14
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多