2013-06-28 14 views
6

https://github.com/kripken/lua.vm.js/issues/5 numaralı telefondan stackoverflow'a göndermek istediğim bir sorun. Daha yüksek maruz kalma durumunda daha hızlı bir cevap alabilirim. Sadece sorumun açık bir şekilde anlaşıldığından emin olmak için, yeniden düzenleyeceğim. Aşağıdaki örneklerden geri arama verilerine nasıl ulaşabilirim?lua.vm.js ajax geri çağırma tetikleniyor ancak veriler döndürülmedi

sorunu gönderildi:

tarayıcınızın JavaScript değiştirilmesi için büyük bir potansiyele sahip yazılımın fantastik parçası (lua.vm.js bir olduğunu)!

Posta listesi, wiki, sorunlar vs.'den toplanan birkaç kod parçası. Her şey algılanan performans etkisi olmadan kutudan çıkar. Ben sadece JQuery ajax çağrıları ve WebSocket dönen mesajında ​​geri dönüş değeriyle ilgili problemlerim var.

Örneğin (script_example.html aşağıya bakınız):

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 

bir çözüm yük() fonksiyonu kullanılarak aşağıdaki

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 geçer (lua.vm. bakınız js git depo):

<!-- 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> 

yukarıdaki örneklerde yüklü glossary.json dosyası:

{ 
    "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" 
      } 
     } 
    } 
    } 
} 

cevap

2

Maalesef, geri gelmeyi ve çözümü bildirmeyi unuttuğumu fark ettim.

jQuery 1.5 sürümünden jQuery'nin tüm Ajax yöntemleri, XMLHTTPRequest nesnesinin bir üst kümesini döndürür. Bu jQuery XHR nesnesi veya $ .get() tarafından döndürülen "jqXHR", tüm özellikleri, yöntemleri veren Promise arabirimini uygular.

Örneğin, responseText ve responseXML özelliklerini içerir. getResponseHeader() yöntemi. Buna dayanarak

, aşağıdaki kod çalışır ve döner geri arama verileri (responseText):

aynı zamanda doğrudan XMLHTTPRequest sarıcı fonksiyonu ile tamamen jQuery atlatabilir
local jq = js.get("$") 
local jqxhr = jq.get("/glossary.json") 
jqxhr.done(function() print(jqxhr.responseText) end) 

Bir:

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) 
İlgili konular