3

Please help - I have the following code which returns the variable 'results' in an alert. I want to use this variable outside of the function, and cannot get a callback, or outside declaration to work successfully. I'm sure I'm being a muppet, so I apologise for this basic question.....

<script src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script>

$(function () {
    $.ajax({
        type: 'POST',
        dataType: 'jsonp',
        url: 'https://creator.zoho.com/api/json/my-company-culture/view/PageFeed_Report?scope=creatorapi&authtoken=d670dc68ac0f6d7ca389e7b206a25045',
        success: function (results) {
            var raw_result=JSON.stringify(results);
            alert(results);
        }
    });
});

 </script>
MarkE
  • 51
  • 1
  • 1
  • 5

2 Answers2

6

The jQuery 1.5+ way of resolving this is by using deferred objects:

var res;

var ajax = $.ajax({
    type: 'POST',
    dataType: 'jsonp',
    url: ...
}).done(function(results) {
    // you may safely use results here
    res = results;
    ...
});

// you cannot safely use 'res' here, only in code
// invoked via a 'done' callback on the `$.ajax` promise

console.log(res);    // undefined

It would be incorrect to attempt to copy results into some other variable in the outer scope unless you have ensured that no other code tries to access that variable until the AJAX call has completed.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • This works, and looks a whole lot neater than my original code, but I don't know how to use the 'results' variable elsewhere in my script. – MarkE Nov 27 '13 at 08:04
  • @MarkE the whole point is that you _can't_! If other stuff depends on `results`, it needs to be passed as a parameter to a function called from within the callback. Alternatively you need to arrange that whatever code will use that value cannot possibly be invoked until after the `.done` callback has been completed. – Alnitak Nov 27 '13 at 11:38
  • I know this in not clean - but I fixed my problem by using the results to populate and open a new page - recovering the variables from the URL in the same way as using post. It did exactly what I needed it to do in this form - and I couldn't have done it without all the help. Many thanks. – MarkE Nov 27 '13 at 22:53
  • Thanks @Alnitak, you have no idea what this answer means to me! For some reason I also had to set async=false to get the results appended to the div. – Iwan Ross Sep 03 '19 at 09:54
  • @nawissor read the top answer on the question that's linked above as a duplicate: "How do I return the response from an asynchronous call?" – Alnitak Sep 03 '19 at 12:46
3

Well, why not:

    <script src='http://code.jquery.com/jquery-1.9.1.js'></script>
    <script>
    function do_the_stuff(smth) {
         console.log(smth);//or do whatever you want to do with this data
    }
    $(function () {
        $.ajax({
            type: 'POST',
            dataType: 'jsonp',
            url: 'https://creator.zoho.com/api/json/my-company-culture/view/PageFeed_Report?scope=creatorapi&authtoken=d670dc68ac0f6d7ca389e7b206a25045',
            success: function (results) {
                var raw_result=JSON.stringify(results);
                do_the_stuff(results);
            }
        });
    });

    </script>

That works fine for me. I don't see any problem.

haldagan
  • 911
  • 5
  • 16