`cc.loader.load` and `cc.loader.loadJson` act differently in HTML5 and JSB

I’m using cocos2d-js v3.0beta.

I have tried cc.loader.loadJson like below:

cc.loader.loadJson(res.Config_json, function(err, data){
	if (err) {
		return cc.log("load failed");
	} else {
		cc.log("config: " + data);// data is the json object
		self.config = data;
	}
});

It works in JSB, but has a delay in HTML5. In HTML5, the json file is loaded after other code need to read its data, which leads to such error of “Cannot read property 'stageCount' of null'”. If I use:

cc.loader.load(res.Config_json, function(results){
    self.config = results[0];
    cc.log("config.stageCount--->" + self.config.stageCount);
});

Then, it work well and has no delay in HTML5, but when compile for win32 using JSB, it report error: TypeError: self.config is null. It seems to be unable to load the JSON file using cc.loader.load in JSB.

cc.loader.load and cc.loader.loadJson act differently in HTML5 and JSB. What’s the problem? How can I deal with it? If I want to it work in both HTML5 and JSB, what can I do?

I met the same problem

can anybody help us?

I will check this out tomorrow

Hi @camel_liu, @timdo

The first situation isn’t an issue, cc.loader.loadXXX functions are asynchronous, so you can’t request the result directly after the function call, you need to rely on the callback function to retrieve the data. It worked in jsb only because there is no real loading in jsb.

The second one is an issue, and I just fixed it in this commit, it will also be merged to official cocos2d-js very soon.

Thanks for your feed back

tks i will try it

Hi @pandamicro i found same problem in cocos2d js RC0 please check it

code like

        cc.loader.load(res,function(results){
            self.doStartUp(results);
        });

res is resource list by json

      cc.log("data is:" + results[0]);  //it's null by results[0]

Yes, it have just been fixed and will be merged in RC2 version, so previous version get the same issue. Sorry for that