How to load json files from remote server?(mobile)

Hi i am trying to load a json file from my server… so i tried

cc.loader.load({id: '', type: 'json'}, function (err, tex) {

cc.loader.load('', function (err, tex) {

these codes are working on web platform… but not on android…
and i tried looking at cc.loader.getItems() my json file is not there…
why it its not working on android? :tired_face:

someone help? :frowning:

Instead of waiting for someone to help you, and posting (almost) the same message on different theads, did you considered searching the forum for a similar question?

By doing that, you would have find an answer much more quickly…

It took me about 3 seconds to find this post:

(in short, add the .json file to your project, and use cc.url.raw to build a valid URL)


@cocosdan i already did this… and i already got an alternative way
but as much as possible we want the process of loading our resources be unified…
and the fact that i posted 2 same entries in the forum for this, means already did everything i could and now desperate. my apologies sir…
its just disappointing that we are having issues in this small matters which cocos creator should already polished or fixed

i suggest that before adding new features to cocos creator they might as well consider fixing the glitches first…

Please don’t blame Cocos Creator team, they are doing a fantastic job !!

By the way, your problem is not really Cocos (Creator) related: you can do what you want in plain JavaScript, you don’t need to use Cocos Creator API for that.

Just ask Google and you’ll find plenty of examples.


haha im not blaming anyone :smile:
i think your missing my point… may be its because of my English sorry…
and nope

cc.loader.load('', function (err, tex) {

is not working on android… sure of it… im not sure why been testing it for days…
BTW before i post or ask anyone, i make sure i do my research first and test it for DAYS…
im posting on forums not just for my self… but for others that may also encounter these kind of problems…

Has anyone got cc.loader.load() to work for remote files? @cocosdan’s solution is not an answer for loading remote files.

Sorry for the inconvenient, I think you have met a bug that have been fixed in 1.2, I will do a test and give you a feedback

@pandamicro thanks ill wait for your reply…
we badly need it… as it can change our system design…

:sweat_smile: I thought you failed to load a remote image, but just saw you want to load a remote json file.

So here is the thing, cc.loader.load support remote image url, but it doesn’t support remote text files. For text files, please use the following code:

var xhr = cc.loader.getXMLHttpRequest();"GET", "http://your-json-url", true);
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {

We needed to do the same thing, and while I was hoping there was a nice convenient method with cc.loader, it was looking premature, so we had to use jQuery to pull it off… Was so thrilled when we got it working, there are advantages to Cocos being JavaScript… First we had to load jQuery using this:

var script = document.createElement('script');
script.src = '';
script.type = 'text/javascript';

Which I put outside the cc.Class. Then for the loading, we made a js class that went something like this:

//This is the communication class that connects to the server
var Client = (function () {
    function Client() {
        //The location of the API
        this.baseUrl = "http://server.*.net/api/game/";
    //This function sets up a new game on the server.  
    Client.prototype.setup = function (playerToken, callback) {
        var _this = this;
        //using the player token call the webserver and then use the results in the callback
        $.getJSON(this.baseUrl + "Setup/" + playerToken, function (c) {
            var gs = jQuery.extend(new GameState(), c);
            _this.gameSetup = gs;

There might be more to it, but using $.getJSON did the trick nicely. Don’t know about the getXMLHttpRequest method, but that might work too. Have fun, good luck…

@skquark thank you for your suggestion…
but i dont think it will run on native build… is it? :smiley:

I don’t see why it wouldn’t work on a native build, I’ve been building to Web Mobile and Web Desktop and it works great, but I honestly haven’t tried compiling it to Android since I’ve been having difficulties with my Environment Path settings that I haven’t gotten around to resolving yet. Still, I expect it to work since it’s all JavaScript anyways, and it was said that Cocos is jQuery compatible. If it doesn’t work, I’m gonna be in big trouble since I based the whole project over JSON communication with the web server to send the gamestate, moves and everything, and it’s been coming along well so far. Does anyone else think there’ll be a compatibility problem?

I don’t think jQuery works with cocos native build, because jQuery rely on DOM, and DOM is not JavaScript standard, it’s implemented by browsers’ layout engine (Webkit or others).

So browsers have mainly two parts: the javascript engine and the layout engine, in cocos native build, we only have pure javascript environment, anything rely on the layout engine will break the native build unfortunately.

@skquark Where did you see that ?

Anyway if you only need JSON communication, it’s totally ok to retrieve them with XMLHttpRequest.

Hi pandamicro,

Your code works good, but how can I handle no internet connection (Android/iOS)?

It seems the callback onreadystatechange is not called. All I see in the log is “ResponseText was empty, probably there is a network error!”. I am using google ads and not sure what logs this error.


Found: I have to use onerror.

Correct, actually it’s a browser standard API, you can found its usage here: