Ajax problem

  • 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

  • Try Construct 3

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

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

  • Official Ajax plugin works fine. You might reference it.

  • $.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) {
    	        
    });[/code:2ey1evvz]
    
    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)".
  • Acey

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

    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) {
              
    });[/code:1r8riod9]
  • Acey

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

    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) {
              
    });[/code:2xc0jwrl]
    

    Can you maybe explain why this should work, so you just set var self to this?

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

  • I havne't tried it, but that sounds correctly, thank you

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