0

I am trying (rather unsuccessfully) to change a variable from the outer context from within the onLoad callback of a XMLHTTPrequest.

The problem is, that within the onload function, this is the local context XMLHTTPrequest which contains the response object. (That is good, because I also need to read the response object)

But at the same time, I need to change a variable justRegistered from the outer context. I have tried to bind the outer context. This left me with the problem that I could not access the response object.

I am developing in AngularJS with the Ionic framework.

I would be very happy if someone knows how to handle this!! Thank you! Here my code:

var request2 = new XMLHttpRequest();

                    request2.open('POST', url2, true); 
                    request2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                    request2.onload = function() {
                      if (this.status >= 200 && this.status < 400) {
 
                        var response2 = JSON.parse(this.response); // `this` is XMLHTTPrequest
                        var error_email = true;
                        error_email = response2["error"];

                        if(error_email){
                          alert("Error");
                        }else{
                          this.justRegistered = true; // `this` should be from outer scope (not XMLHTTPrequest)
                        }
                      }
                      };
                      request2.send(); 
Moritz
  • 1,710
  • 1
  • 8
  • 13

0 Answers0