Home > database >  what alternative for ajax async deprecated feature?
what alternative for ajax async deprecated feature?

Time:12-23

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

CodePudding user response:

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

CodePudding user response:

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/

  •  Tags:  
  • ajax
  • Related