Jump to content

Loading json file onFileError signal


glantucan
 Share

Recommended Posts

Hi

I found out that when loading json files with game.load.json() if the file is not found the onFileError signal fires after the Phaser.Loader.jsonLoadComplete() function is executed which produces a parsing error as the server is responding with HTML

<!doctype html><title>404 Not Found</title><h1 style="text-align: center">404 Not Found</h1><hr/><p style="text-align: center">WebStorm 10.0.4</p>

This avoids the detection of the actual error. So I can do nothing to amend it.

 

Does anyone knows how to solve this?

 

Thanks in advance

Link to comment
Share on other sites

Nobody?

 

Let me explain a little what I am trying to achieve. I am building an educational game with several minigames inside (drag and drop, card match, etc. most of them point and click )

 

As the games all are the same on each subject it tries to teach the configuration files are almost the same for each subject. But sometimes some texts don't fit in the space they have reserved, so I have to change the fon size, or for a particular subject there is a different background to load, etc., etc. There can be a lot of mimimal changes like those.

 

Being so, I thought to keep a small json conf file for each common gameObject in the minigames. Each of those have a default version that is always loaded and can have a 'per subject' version that takes care of the difference.

 

But I am stuck with the error explained above, I planned to use the default conf files as fallbacks when the 'per subject' version wold get a FIleError event, whic I cannot get in time because the JSON parsing error gets in the way.

 

Hope it is clearer now

 

Thanks

Link to comment
Share on other sites

I'm thinking your best bet would be to include those files in the page and skip the loader entirely. Depending how many of those files there are or how big they are your load time probably wouldn't be increased too badly. E.g. think about the size of one of your JSON files vs. one of your images; I bet the JSON files are tiny, relatively speaking.

 

Failing that, make one JSON file named "overrides.json" (or something) and load that. Make each of its keys the name of the object you're overriding.

 

Failing even *that*, customize the 404 page for whatever server you're using to either return a blank body during 404, or return a minimal JSON doc. That seems unnecessarily brittle, though – you'd have to worry about what happens to your game when someone changes a config or you move it to a different server.

Link to comment
Share on other sites

Thanks drHayes

 

Sort of already did wht you suggest.

 

I always load a defaults json and a 'per subject' json with all the configuration so there is always afile to load in both cases. I don't want to include them in the page as it is always the same and also prefer to have things as modular as possible. And you are right, changing 404 page on the server didn't feel like the solution.

 

Anyway, do you know if this behavior is normal, I mean, a feature or may be a Phaser bug?

Link to comment
Share on other sites

"Normal", I guess? You're telling Phaser to load JSON but your server is handing it HTML. ¯\_(ツ)_/¯

 

Maybe it's because I don't know so much about http but shouldn't I receive the http error. Or it's a question of how the server is configured too?

Link to comment
Share on other sites

Phaser creates an XMLHttpRequest to load JSON files and specifies the response type as "text". An XHR's onerror handler only fires if there's an error at the network level, i.e. the server is completely unreachable or the user's network cable is disconnected. If the server successfully responded, even with a 404, then the XHR's onload will fire... which means that Phaser gets a crack at interpreting the results of the XHR which, in your case, is an HTML page. Phaser doesn't look at the status of the XHR, only the response.

 

Hence the shrug. Does this make sense?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...