2013-10-31 27 views
0

我正在構建一個金字塔web應用程序,我正在嘗試實施JavaScript降價編輯器EpicEditor以編輯降價文件。AJAX Javascript字符串和Pyamid

$.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,除非該字符串永遠不會顯示在頁面上,這看起來很順利。

回答

3

你應該把你的代碼中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(); 
    } 
}) 
+0

完美,謝謝! – sJohnsonStoever