Export problem JSON.parse(request.responseText)

For developers using the Construct 2 Javascript SDK

Post » Sun Jan 10, 2016 8:30 am

Am I doing something wrong?
In preview it works, but when export, the json contents it´s not showed in the line marked below.

Code: Select all
Acts.prototype.LoadAddress = function (zipcode){
      var request = new XMLHttpRequest();
      request.open('GET', '//viacep.com.br/ws/'+zipcode+'/json/', true);

      request.onload = function() {
         cr.logexport(request.status); // 200
         if (request.status >= 200 && request.status < 400) {
            cr.logexport(request.responseText); // show json content
            this.dataJson = JSON.parse(request.responseText);
      ----> cr.logexport(this.dataJson.logradouro); // undefined when exported


Demo: http://kiduca.com/frc/zipcodePlugin/
When click in "Find address", the form should be loaded with the address from zip code.
B
18
S
4
G
1
Posts: 143
Reputation: 1,868

Post » Mon Jan 11, 2016 2:23 pm

Is there a better way to get json content inside a plugin that works when exported or is common to use JSON.parse(request.responseText)?
B
18
S
4
G
1
Posts: 143
Reputation: 1,868

Post » Mon Jan 11, 2016 3:23 pm

Maybe the issue came from minify. Try request["responseText"]. BTW, this.dataJson might be wrong, try self.dataJson. Sure it needs var self=this;
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Thu Jan 14, 2016 7:18 am

Nope.

Code: Select all
cr.logexport(request["responseText"]);
cr.logexport(self.dataJson);
self.dataJson = JSON["parse"](request["responseText"]);
cr.logexport(self.dataJson.logradouro);

first line returns

Code: Select all
{
  "cep": "18061-000",
  "logradouro": "Rua Humberto de Campos",
  "complemento": "",
  "bairro": "Jardim Zulmira",
  "localidade": "Sorocaba",
  "uf": "SP",
  "ibge": "3552205",
  "gia": "6695"
}

Second line returns "Object {}"
Fourty line returns "undefined"

Works in preview, but not when exported for HTML5 :(
B
18
S
4
G
1
Posts: 143
Reputation: 1,868

Post » Thu Jan 14, 2016 7:29 am

cr.logexport(self.dataJson["logradouro"])

The reason is equal to request["responseText"].

obj.name after minify will become obj.a for example, uses obj["name"] to prevent name changing.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Thu Jan 14, 2016 7:34 am

Yep!
Thanks @rexrainbow!
B
18
S
4
G
1
Posts: 143
Reputation: 1,868

Post » Thu Jan 14, 2016 7:51 am

"The main thing is to always use dot syntax (Object.property) rather than bracket syntax (Object["property"]) in your own code. All properties using dot syntax are changed by Closure Compiler, but none of the properties in bracket syntax are changed."


https://www.scirra.com/manual/22/runtime-overview

Is it telling to always use dot syntax or I misundestood??
I remember that i´ve read somewhere about it (the reason I´m using dot).

The same time that it tells to use just dot syntax, the next sentence tells that dot syntax is changed when minified but bracket syntax not.
B
18
S
4
G
1
Posts: 143
Reputation: 1,868

Post » Thu Jan 14, 2016 8:57 am

Properties only used inside engine/plugins could be dot syntax (Object.property), otherwise use bracket syntax (Object["property"]) -- For example, read properties of an object which came from a JSON string , or network, or call external javascript function call.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests