Closed antonh closed 10 years ago
Sorry, I just realized I forgot to come back and report the solution.
As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," returned by $.get() implements the Promise interface, giving it all the properties, methods,...
For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.
Based on this, the following code works and returns the callback data (responseText):
local jq = js.get("$")
local jqxhr = jq.get("/glossary.json")
jqxhr.done(function() print(jqxhr.responseText) end)
I can't seem to do anything with the jqxhr
object returned. Trying to access any of its properties results in !Unsupported!
including jqxhr.done
.
There seems to be something very odd going on with lua <--> js interaction. I'll submit this as a new issue.
Basically, this is what I'm seeing:
local jq = js.get("$")
local jqxhr = jq.get("./glossary.json")
jqxhr.done(function() print(jqxhr.responseText) end)
The above works and it prints the parsed output as expected. However, if I add some extra code using jq object then things start to mysteriously fail:
local jq = js.get("$")
local version = jq().jquery
print(version, '********')
local jqxhr = jq.get("./glossary.json")
jqxhr.done(function() print(jqxhr.responseText) end)
version
of jquery gets printed but jqxhr.done
will have a string value of !Unsupported!
. Switching the jquery statement with jq.get
will cause the reverse:
local jq = js.get("$")
local jqxhr = jq.get("./glossary.json")
local version = jq().jquery
print(version, '********')
jqxhr.done(function() print(jqxhr.responseText) end)
Now, version will contain the !Unsupported!
string and jqxhr.done
succeeds with the json parse.
Is still an issue with HEAD?
Thanks for your work! It's working now with a slightly different syntax:
<html>
<head>
<title>script example</title>
</head>
<script src="lua.vm.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/lua">
local jq = window.jQuery()
print("version", jq.jquery)
print("version second call", jq.jquery)
local jqxhr = window.jQuery:get("/glossary.json")
jqxhr:done(function() print(jqxhr.responseText) end)
</script>
</html>
Fantastic piece of software with a huge potential for replacing JavaScript in the browser!
A few snippets of code collected from the mailing list, wiki, issues, etc. Everything works out of the box with no perceived performance impact. I only have problems with callback return values on JQuery ajax calls and WebSockets returned message.
For example (see script_example.html below):
A workaround using the load() function:
The following goes into script_example.html:
The glossary.json file loaded in the examples above: