Ajax problem

For developers using the Construct 2 Javascript SDK

Post » Sat Mar 19, 2016 6:58 pm

Hi
I've used like 4 hours to get some ajax to work, but it doesn't seems to work..
And my not working i mean that i can set (this.data) in the .done(result) function, when the ajax is done, and the data is pulled.
I finally ended up using HttpRequest, but why doesn't this work ?

-Jesper
Acey
ImageImage
B
19
S
10
G
2
Posts: 51
Reputation: 2,070

Post » Sun Mar 20, 2016 4:46 am

You've literally given zero information about what's working, and what's not working.

I've used the AJAX Object successfully, quite a bit.

If you give a more concrete example, I'm sure we can all help.

-john
B
16
S
4
G
1
Posts: 19
Reputation: 1,372

Post » Sun Mar 20, 2016 5:42 am

Official Ajax plugin works fine. You might reference it.
B
108
S
26
G
272
Posts: 4,472
Reputation: 152,279

Post » Sun Mar 20, 2016 9:11 am

Code: Select all
$.ajax({
   url: "http://api.openweathermap.org/data/2.5/weather?apikey=" + apikey + "&lat=" + lat + "&lon=" + lng,
    method: "get"
}).done(function (res) {
   this.result = res;
          
   console.log(res);
}).error(function (result) {
          
});


I tried this code. But i wasn't able set "this.result" to anything from inside the done function.

I've looked at the Ajax plugin, and that uses HttpRequest too, just wondered why this didn't work, since i was able to pull the data correctly when i tried "console.log(res)".
ImageImage
B
19
S
10
G
2
Posts: 51
Reputation: 2,070

Post » Sun Mar 20, 2016 9:43 am

@Acey

Add "var self=this;" , and replace "this" by "self" in callback function.

Code: Select all
var self=this;
$.ajax({
   url: "http://api.openweathermap.org/data/2.5/weather?apikey=" + apikey + "&lat=" + lat + "&lon=" + lng,
    method: "get"
}).done(function (res) {
   self.result = res;
         
   console.log(res);
}).error(function (result) {
         
});
B
108
S
26
G
272
Posts: 4,472
Reputation: 152,279

Post » Sun Mar 20, 2016 10:29 am

rexrainbow wrote:@Acey

Add "var self=this;" , and replace "this" by "self" in callback function.

Code: Select all
var self=this;
$.ajax({
   url: "http://api.openweathermap.org/data/2.5/weather?apikey=" + apikey + "&lat=" + lat + "&lon=" + lng,
    method: "get"
}).done(function (res) {
   self.result = res;
         
   console.log(res);
}).error(function (result) {
         
});


Can you maybe explain why this should work, so you just set var self to this?
ImageImage
B
19
S
10
G
2
Posts: 51
Reputation: 2,070

Post » Sun Mar 20, 2016 10:38 am

I might not explain clearly.
"this" had been override in callback, it was not the same object as you expect. So using another variable to store the "this" reference.
B
108
S
26
G
272
Posts: 4,472
Reputation: 152,279

Post » Sun Mar 20, 2016 1:35 pm

I havne't tried it, but that sounds correctly, thank you :)
ImageImage
B
19
S
10
G
2
Posts: 51
Reputation: 2,070


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests