Qml / Javascript XMLHttpRequest issues in Qt 5 beta 2
-
As of the beta 2 release I'm seeing some strange issues when using XMLHttpRequest to issue various rest requests to a device. Specifically it seems that the callback function I've specified for onreadystatechange is not always called.
The general structure of the code goes like this. I have a model object defined in javascript and this object has several functions for get, put, etc that allow the model data to be in sync with that of a server which is the source of this information. In the get function I have the following (distilled version):
@model.prototype.get = function (resource, callback) {
var request = new XMLHttpRequest();request.onreadystatechange = function () { if (request.readyState === 4) { if (request.status >= 200 && request.status < 300) { // do stuff here } else { // do other stuff here } } }; request.open("GET", "http://" + this.ipAddress + "/rest/" + resource, true); request.withCredentials = true; request.timeout = model.timeout; request.send();
}@
This code lives in foo.js and is then imported into bar.qml. In bar.qml I trigger a call to get through a button press, the call succeeds and with log messages I can see that everything executes correctly to issue the get, however the onreadystatechange callback is never executed.
Now, if I move all of this code into bar.qml it executes perfectly every time.
Anyone have any idea what is going on? This really smells of an "interesting" javascript bug in beta 2 but I haven't been able to distill this down any further to determine the cause.
Thanks,
Chris. -
I'm not 100% certain, but it may be due to having no valid evaluation context at the time of the callback. In most cases, this doesn't actually cause any issues (because the code which runs doesn't necessarily depend on the evaluation context), but some functions (like Qt.createObject()) will crash and others will behave badly.
See fdeee38b in qtdeclarative.git.
File a bug with a minimal testcase if you're sure that it's doing something wrong.Cheers,
Chris. -
Hi,
At least here it is stated that the callback should not be denifed before calling "open"
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#Properties