2013-06-28 54 views
6

这是我在https://github.com/kripken/lua.vm.js/issues/5提出的问题,我想提交给stackoverflow。考虑到更高的曝光率,我可能会得到更快的答案。为了确保我的问题清楚地被理解,我会重申它。我如何从下面的例子中得到回调数据?lua.vm.js ajax回调开火但数据没有返回

问题提交:

(lua.vm.js是:a)与在浏览器中的JavaScript替代潜力巨大奇妙的软件!

从邮件列表,维基,问题等收集的一些代码片段。一切都开箱即用,没有明显的性能影响。我只有JQuery ajax调用和WebSocket返回消息的回调返回值的问题。使用load()函数

js.run('$.get("/glossary.json", function(data) { console.log(data); });') -- this works 
jq.get("/glossary.json", function(data) print(data) end) -- the callback is firing, but data is not returned 

一种解决方法:

例如(见下面script_example.html)

jq('#result').hide().load("/glossary.json", function() print(jq('#result').html()) end) -- this works because after the callback is fired, we just collect the result from the result div 

下面进入script_example.html(见lua.vm. js git repository):

<!-- begin script tag example --> 

<script src="lua.vm.js"></script> 
<script src="jquery-1.10.1.js"></script> 

<!-- 
    Simplest web server for serving static files 
    python -m SimpleHTTPServer 8080 
--> 

<script type="text/lua"> 
-- Print contents of `tbl`, with indentation. 
-- `indent` sets the initial level of indentation. 
function tprint (tbl, indent) 
    if not indent then indent = 0 end 
    for k, v in pairs(tbl) do 
    formatting = string.rep(" ", indent) .. k .. ": " 
    if type(v) == "table" then 
     print(formatting) 
     tprint(v, indent+1) 
    else 
     print(formatting .. tostring(v)) 
    end 
    end 
end 

-- function test() 
-- return 'ok' 
-- end 
-- for i=1,5 do 
-- js.global.alert(test()) 
-- end 

local jq = js.get("$") 
-- jq('body').append("plop").click(function() js.global.alert("plop click") end) 
-- local version = jq().jquery 
-- js.global.alert(version) 
-- jq('#result').load("/glossary.json") 
jq('#result').hide().load("/glossary.json", function() print(jq('#result').html()) end) 
-- jq.get("/glossary.json", function(data) print(data) end) -- callback is firing, but data is not returned 
-- js.run('$.get("/glossary.json", function(data) { console.log(data); });') 

-- local ws = js.new.WebSocket("ws://echo.websocket.org/?encoding=text") 
-- ws.onopen = function() 
-- print("connected!") 
-- ws.send("Rock it with HTML5 WebSocket") 
-- end 
-- ws.onclose = function() 
-- print("disconnected") 
-- end 
-- ws.onerror = function(error) 
-- print(error) 
-- end 
-- ws.onmessage = function(e) 
-- tprint(e) -- using tprint() because an empty table is returned instead of the message 
-- ws.close() 
-- end 
</script> 

<!-- end script tag example --> 

<div id="result"></div> 

上面的例子中加载的glossary.json文件:

{ 
    "glossary": { 
    "title": "example glossary", 
    "GlossDiv": { 
     "title": "S", 
     "GlossList": { 
      "GlossEntry": { 
       "ID": "SGML", 
       "SortAs": "SGML", 
       "GlossTerm": "Standard Generalized Markup Language", 
       "Acronym": "SGML", 
       "Abbrev": "ISO 8879:1986", 
       "GlossDef": { 
        "para": "A meta-markup language, used to create markup languages such as DocBook.", 
        "GlossSeeAlso": ["GML", "XML"] 
       }, 
       "GlossSee": "markup" 
      } 
     } 
    } 
    } 
} 

回答

2

对不起,我刚才意识到我忘了回来报告解决方案。

从jQuery 1.5开始,所有jQuery的Ajax方法都返回一个XMLHTTPRequest对象的超集。这个jQuery XHR对象,或者由$ .get()返回的“jqXHR”实现了Promise接口,赋予了它所有的属性,方法......

例如,它包含responseText和responseXML属性,以及一个getResponseHeader()方法。

在此基础上,将下面的代码工作,并返回回调数据(responseText的):

local jq = js.get("$") 
local jqxhr = jq.get("/glossary.json") 
jqxhr.done(function() print(jqxhr.responseText) end) 

人们也可能有直接的XMLHTTPRequest包装功能完全绕过jQuery的:

local xhr = function(method, url, callback) 
    local xhr = js.new.XMLHttpRequest() 
    xhr.onreadystatechange = function() 
    if xhr.readyState == 4 and xhr.status == 200 then 
     return callback(xhr.responseText) 
    end 
    end 
    xhr.open(method, url) 
    xhr.send() 
end 

xhr("GET", "/glossary.json", function(data) print(data) end)