0

Using the following ajax function to call an API endpoint which returns data in JSON format does not change the value of results from undefined to what ever is in the json['info'] but when changing async to false it does.

Inspecting the webpage containing this function it shows the this feature is deprecated and does not suggest anything else.

How do you read the json data with out writing more code to parse the data returned from the server?

function update_info()
{
    var results ;
    $.ajax({
        type: 'GET',
        url : "ip:port/api/info",
        async:true,
        success: function(data) {
            results = data['info'];
            //console.log(results);
        }
    });
    console.log(results);
    return results;
}   

This does not seem to fit my goals as the data is returned from an API and not webpage source code is returned to the ajax.

George Sun
  • 85
  • 6
ahmad
  • 9
  • 5
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – David Dec 22 '21 at 20:33
  • I don't think so because the function does not return the info needed and this still does not provide a work around for not using `async: true` correct me if I am wrong – ahmad Dec 22 '21 at 20:41
  • 2
    You are wrong. The problem you're trying to solve is how to treat an asynchronous operation as though it were synchronous. This is the wrong way to approach the problem. Embrace the asynchronous nature of the operation. In the code shown the specific case is that you're making an AJAX call within a function and want to return the result of that call from that function. The question and answers in the linked duplicate use that exact scenario for examples. There's no "workaround" for asynchronous operations, the solution is to keep them asynchronous. – David Dec 22 '21 at 20:43

2 Answers2

1

As noted by David you should use asynchronous calls

       function update_info()
       {
            
            return $.ajax({
                type: 'GET',
                url : "ip:port/api/info",
                async:true,
                success: function(data) { data;}
            });}
       async function f1() {
            var results = await update_info();
            console.log(results['info']); 
       }
       f1();

this will return the ajax call when it is done and then you can grab the info data from it

Weed Cookie
  • 579
  • 1
  • 3
  • 11
1

You may use both approaches, and you can use .then or await.

I attached a source code for the following examples below.

ajaxRequest("https://httpbin.org/ip").then(result => {
  console.log('Your PC Address is:' + result.origin);
});

let myPcRes = await ajaxRequest("https://httpbin.org/ip");
console.log('Your PC Address is from the await approach: ' + myPcRes.origin);

you need to compare the results to the $.ajax

and return the response from of success method.

I hope you will find it helpful, https://jsfiddle.net/tru5k6qz/

Oren Hahiashvili
  • 340
  • 1
  • 4
  • 6