0

Accessing (Modifying) a class member inside an onload function

I have stuck with a problem here, I made a class named Ajax, in its constructor I've made a member named 'response' [this.response], Then I created the xhr object also a member. Then I created a method named 'makeAjax(page, method)', which takes two arguments one for page and another for method of request. Then, now when I used onload method, the problem starts. I'm want to access my this.response member inside the this.xhr.onload. Im a newbie, so I'm bad with some bindings Error: Uncaught TypeError: Cannot assign to read only property 'response' of object '<#XMLHttpRequest>' at XMLHttpRequest.xhr.onload https://code.sololearn.com/WBjlb1JFa0dJ/?ref=appError: Uncaught TypeError: Cannot assign to read only property 'response' of object '<#XMLHttpRequest>' at XMLHttpRequest.xhr.onload

16th Jan 2019, 4:25 AM
Hemath Kumar
Hemath Kumar - avatar
14 Respuestas
+ 5
Hemath Kumar As Calviղ explained, you can use a call back function to call another method after api.response has been assigned a value. You can also attempt to load it after waiting a second or two. Here are examples of both options. Just review the console output. https://code.sololearn.com/WKOY3sSO1fkW/?ref=app
16th Jan 2019, 8:56 AM
David Carroll
David Carroll - avatar
+ 3
Oh it's all good. David Carroll I am glad you have helped to clear the doubts.
16th Jan 2019, 9:29 AM
Calviղ
Calviղ - avatar
+ 2
You have to use callback onload, to get the data.
16th Jan 2019, 8:25 AM
Calviղ
Calviղ - avatar
+ 2
Hemath Kumar I only clarified one final part. Credit really should go to Calviղ. I'm glad you have a better understanding. 😉👍
16th Jan 2019, 9:12 AM
David Carroll
David Carroll - avatar
+ 1
Hemath Kumar i cannot ignore your typo, you have serious error, xhr.send should be outside of onload.
16th Jan 2019, 5:43 AM
Calviղ
Calviղ - avatar
+ 1
You no longer can use this.xhr in the feedback onload, add bind function to the feedback, .bind(this.xhr) Check out the code here Add a status 200 checking too. https://code.sololearn.com/WIobyxuF18Dt/?ref=app
16th Jan 2019, 5:45 AM
Calviղ
Calviղ - avatar
+ 1
Then you just .bind(this) Check out the code again.
16th Jan 2019, 6:22 AM
Calviղ
Calviղ - avatar
+ 1
Hemath Kumar onload feedback output cannot get immediately due to network delay, this is why you need onload callback. If you really need to get the this.reponse outside of onload fb after xhr.send, add some estimated delay before get the value.
16th Jan 2019, 7:19 AM
Calviղ
Calviղ - avatar
+ 1
Cleared my doubts! Thanks for both Calviղ David Carroll
16th Jan 2019, 9:04 AM
Hemath Kumar
Hemath Kumar - avatar
0
Some syntax errors on your code. https://code.sololearn.com/WIobyxuF18Dt/?ref=app
16th Jan 2019, 5:12 AM
Calviղ
Calviղ - avatar
0
Ignore them... That's just typo err.... I need the solution for binding
16th Jan 2019, 5:13 AM
Hemath Kumar
Hemath Kumar - avatar
0
And I need something like this.response = this.responseText; Where, this.response comes from the constructor of AJAX Class. How can I achieve that?
16th Jan 2019, 6:15 AM
Hemath Kumar
Hemath Kumar - avatar
0
Okay cool... It's working now... Thanks... But I tried another stuff, I tried to access 'response' member by api.response After the call to api.makeAjax() But it outputs nothing.... What is the problem or where I'm missing?
16th Jan 2019, 6:47 AM
Hemath Kumar
Hemath Kumar - avatar
0
So what I have to do for that? Can you pls help me to learn about it?
16th Jan 2019, 7:59 AM
Hemath Kumar
Hemath Kumar - avatar