简体   繁体   中英

JSON object becomes 'null'

I've been having trouble with my html5 web worker. The web worker postMessage()'sa JSON object to the document-level javascript that invoked the worker, which comes with an onmessage() function.

Apparently the "iterationsjob" key can be accessed as the worker can process the associated number value, but this seems to only work in Google Chrome and it still raises the error:

18Uncaught TypeError: Cannot read property 'iterationsjob' of null

In Firefox, the web worker does not work at all and doesn't seem to be able to access the numbers either, I'm getting a successful ajax call but it seems to bypass the logging steps and raise another error with a separate ajax call. So I hacked together a basic way for workers to log() messages back to the console, and invoked them from within the worker:

var iterations
//get iteration data from the server
p.ajax.post({
    url:'/getdataurl',
    dataType: "json",
    success: function(responseText){
        log("test");
        log("responseText is "+responseText);
        iterations = responseText.iterationsjob; 
        log("iterations is "+iterations);         
    }
});

I expected to see the following:

test
responseText is [object Object]
iterations is 3993547

Instead, I see:

test
responseText is [object Object]
iterations is 3993547
test
responseText is null
Uncaught TypeError: Cannot read property 'iterationsjob' of null

Could anybody explain what the problem is and how to fix it? EDIT: I've uploaded a zip of the source code here . Runs on Google App Engine.

Thanks! any help much appreciated.

Ah, turns out that I needed to add an empty success callback function to the second ajax call. for some reason it calls the first success callback by default if there is none defined...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM