Export problem JSON.parse(request.responseText)

  • Am I doing something wrong?

    In preview it works, but when export, the json contents it´s not showed in the line marked below.

    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[/code:1yldme68]
    
    Demo: [url]http://kiduca.com/frc/zipcodePlugin/[/url]
    When click in "Find address", the form should be loaded with the address from zip code.
  • 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)?

  • Maybe the issue came from minify. Try request["responseText"]. BTW, this.dataJson might be wrong, try self.dataJson. Sure it needs var self=this;

  • Nope.

    cr.logexport(request["responseText"]);
    cr.logexport(self.dataJson);
    self.dataJson = JSON["parse"](request["responseText"]);
    cr.logexport(self.dataJson.logradouro);[/code:3kq8rdks]
    first line returns
    
    [code:3kq8rdks]{
      "cep": "18061-000",
      "logradouro": "Rua Humberto de Campos",
      "complemento": "",
      "bairro": "Jardim Zulmira",
      "localidade": "Sorocaba",
      "uf": "SP",
      "ibge": "3552205",
      "gia": "6695"
    }[/code:3kq8rdks]
    Second line returns "Object {}"
    Fourty line returns "undefined"
    
    Works in preview, but not when exported for HTML5
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Yep!

    Thanks rexrainbow!

  • [quote:h2netk0l]"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.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)