Load json data from other server
Janne

Wade editor load json data perfectly if its load from local folder ( wade online engine ), but if I try to load data from my own web server, it aler "unable to load json file...". It doesent work in wade online editor, but when I move game files to my server, it works fine. 

Question is, is it possible to load json data from other server in wade online editor, it will help a lot to use faster game editor. Thanks!

Comments 1 to 15 (19 total)
Gio

Hi

Yes it is possible to load JSON data from an external server. However, you must set up that external server to accept ajax requests from a different domain, otherwise your browser will block the request for security reasons. It's a silly and annoying security measure if you ask me, but that's what browsers do.

So on your external server, you must set the Access-Control-Allow-Origin header to '*' (or to your game's domain name if you only want to allow requests from that particular domain).

On a Node.js server, you would do something like this:

app.use(function(req, res, next) 
{
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

On different types of server you'd do different things - please refer to this MDN article for more details.

Janne

Thanks for quick reply, that works fine :) Problem is still how to preload external json data. I try to load it in app.js this.load function, but it seems that its not gather data fast enough, while game proceed further and thats why loss data or give error because it havent put any data in variable.

 

Gio

Oh OK, that's not a problem with the file being on an external server - it's just that it's taking a bit longer to load than what you might have expected.

What is happening is that wade.preloadJson() , in the same way as all the other wade.preload functions, does not wait for the operation to finish before moving on. You want to use wade.loadJson() instead.

Janne

Thanks for reply. Problem is still that when I load json-file and after that in callback I try to count data lenght, I get error its not defined. Seem that that havent load quick enough. Is there any way to check if json is loaded properly and after that I collect data to variables. If I use loadjson in different scenes, that would help alot. In my case I try to load Highscore -list from server database and print it in the game scene.

Gio

Well, if you are doing things in the callback it doesn't matter whether it's preloadJson or loadJson: the callback is only ever executed after WADE has finished loading the file.

So there must be something else that's wrong - if you want to post a link to your json data and paste the code that you're using to read it, I might be able to help.

Janne

This works wonderfully!

Gio

If it's valid JSON, yes, there should be no problems. It is possible that the MIME type is not set correctly on the server though, you may want to make sure that the mime header is "application/json".

Janne

Php json loads perfectly :) I am loading html text from database, which contains <br> and <b> html formats. Is there any possibily put html text in wade? Now when I put text in textobject, it show <br> and <b> codes, not convert those to html preview. Shortcut to fix one is Preg_replace <br> to \n works fine, but how about making text bold? Thanks.

Gio

Unfortunately those tags are not supported by TextSprite - a TextSprite essentially just draws to a canvas using the Canvas fillText method that only supports plain text.

However this is the open web, and there are lots of free libraries that you can use for this purpose. One such library is rasterizeHTML that you can use to draw HTML into a canvas. In fact, you can do much more than simple <b> tags - rich text with images and whatever you like really.

Once you have done this, you can use that canvas as a source image for a WADE sprite, like this:

wade.setImage('imageName', yourCanvas);

Any Sprites that use 'imageName' as a source image will then use the canvas that you have drawn the HTML into.

Janne

Can you help me with this?
About server communication and "wade.postObject".

If I send data to php like this:
var object = {"dataField": "some text I want to post"};
wade.postObject(url, object, this.onServerResponse_());

Then I try to get that data like this:
$_REQUEST['dataField']

But It doesent work?! I have spend 5 hours to figure it out, how this work. I have try many different variations to gather data from post wihout success. Thanks for your help.

 

 

Gio

It's been a while since I used this with PHP, but have you tried

$obj = json_decode($_REQUEST['data'], true);

I believe data is your JSON string, you can convert it to an object with json_decode on your PHP server. This object will then contain your dataField. I think.

Janne

When request data it always empty?
When send object from wade, there is someting wrong.
----
var object = {"dataField": "some text I want to post"};
wade.postObject(url, object, this.onServerResponse_());
----

I have try this also:
----
var object = "this is some data";
wade.postObject(url, object, this.onServerResponse_());
----

Gio

I don't have a php server handy at the moment to test this, but if you tell me what $_REQUEST contains we might be able to figure it out.

On the client side, object definitely needs to be an object, not a string.

 

Janne

In wade example:
var url = 'http://www.mydomain.com/somefile.php';
var object = {"dataField": "some text I want to post"};
wade.postObject(url, object, this.onServerResponse_());

In php example:
$var = json_decode($_REQUEST['data'], true);

I need $var variable include text like this --> "some text I want to post" ,
now its empty?
I can move some variable info with URL and then get it in php with $_GET["dataField"] command, and it works,
but its too risky in this case. This works in wade --> var url = 'http://www.mydomain.com/somefile.php?dataField=SomeText';

Its always better if it possible to move variable hidden.

Gio

I understand that $_REQUEST['data'] is empty, but what are the contents of $_REQUEST ?

Does it work if, instead of using wade.postObject, you use jQuery.post() ?

Post a reply
Add Attachment
Submit Reply
Login to Reply