1

I am using AMD and Requirejs for implementing the following module:

define({
callWeatherService: function(x, y){

     //var url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=AUD'; // website you want to scrape
    var url = 'http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=' + x + '&CountryName=' + y;

    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + url + '"') + '&format=json&callback=?';  
    $.getJSON(yql,displayData);

    function displayData(data){  
        if(data.query.results){
            result = data.query.results.string.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
            document.getElementById("asmxResult").innerHTML = result;
            // return result;
        }
    }

}

});

I do not want to modify the HTML document in this code but rather return the result. I have tried many different methods for callbacks from other solutions provided in stackoverflow but none seems to be working. e.g. the following code logs the result on the console but returns undefined:

define({


callWeatherService: function(x, y){


    var url = 'http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=' + x + '&CountryName=' + y;

    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + url + '"') + '&format=json&callback=?';  

    var result;

    $(function() {
        var r = GetResults();  
        result = r;
    });

    function GetResults() {

        $.getJSON(yql,function(data){  
            if(data.query.results){
                var output = data.query.results.string.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
            }
            console.log(output);
            return output;
        });

    }

    return result;

}

});

Is this because of using AMD and Requirejs or I am doing something wrong?

Achilleas
  • 11
  • 1

2 Answers2

0

ok your first issue is the return output is in the wrong place so move it here:

    function GetResults() {

        var output;

        $.getJSON(yql,function(data){  
            if(data.query.results){
                output = data.query.results.string.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
            }
            console.log(output);
        });
        return output;
    }

secondly I probably wouldnt do it this way I would create a function to handle the callback rather than using an anonymus callback like so:

    $.getJSON(yql,myFuncToDoSomething(data));

    function myFuncToDoSomething(data){
        if(data.query.results){
            var output = data.query.results.string.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
        }
        console.log(output); //or do something else
    }

it is event driven after all :D hope this helps.

Jake Aitchison
  • 1,079
  • 6
  • 20
0

I think your problem is that you're not using callback functions.

Ajax calls are asynchronous, so the function GetResults can't return something, you have to handle the result inside the callback function.

Instead of returning the value you must do something like this:

callback(output);

or maybe this

callback(data.query.results.string.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ''));

This should get you started, there are many similar SO threads around and Google can take you a step further.

What matters here is that returning values with ajax is trickier than it first appears, some logics are not able to return data because method A which requires something from method B has maybe moved on to another location in method A before it receives what it needed from method B. Best of luck.

Community
  • 1
  • 1
Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • Back from holidays,hence the late reply:) As I understand from both your answers and posts such as http://stackoverflow.com/questions/16020929/return-value-from-getjson-function, http://stackoverflow.com/questions/4200641/how-to-return-a-value-from-a-function-that-calls-getjson there is no way I can return the value but only to use it in the callback method with an a alert or writing to the appropriate html element. I can do this. The problem is that I need to use the value in the main javascript module that retrieves data from other information modules. Is it possible to return the value? – Achilleas Aug 20 '13 at 13:28