0
let choiceContext = [];
let nameList = [];
const data = {
    resource_id: '9329755c-6406-4894-88b7-e7b04ccdfecc',
    limit: 10,
};

$.ajax({
    url: 'https://www.data.brisbane.qld.gov.au/data/api/3/action/datastore_search',
    data: data,
    dataType: 'jsonp',
    cache: true,
    success:
        function sendIN(data){
            $.each(data.result.records, function(recordKey, recordValue) {
                //some code about addding data to choiceContext
                return choiceContext; //assume a result
            });
        }
});
const answer = sendIN();

Hi, in these codes above, there is a function which called sendIN, I just want to use the result of it in out of ajax...but it dose not work.

However, I have tried to use document.Cookie:

let choiceContext = [];
let nameList = [];
const data = {
    resource_id: '9329755c-6406-4894-88b7-e7b04ccdfecc',
    limit: 10,
};

$.ajax({
    url: 'https://www.data.brisbane.qld.gov.au/data/api/3/action/datastore_search',
    data: data,
    dataType: 'jsonp',
    cache: true,
    success:
        function sendIN(data){
            $.each(data.result.records, function(recordKey, recordValue) {
                //some code about addding data to choiceContext
                document.Cookie = choiceContext;
            });
        }
});
const answer = document.Cookie;

It works, but it cannot store enough data, so I need to find another way to achieve this.

Really thanks!

Phoenix
  • 37
  • 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) – Simone Rossaini Oct 06 '21 at 07:20
  • "it cannot store enough data" - what does that mean? Also, why not use any other method, defined properly outside of that AJAX definition, and call that? – Nico Haase Oct 06 '21 at 07:20
  • Because I found that if `choiceContext` has more than 300 elements, it will not be stored in a cookie. – Phoenix Oct 06 '21 at 07:24
  • And how is that related to AJAX? How would that work different if you could call `sendIN` from any other place? – Nico Haase Oct 06 '21 at 07:26
  • You can just call another function from there passing the data and do what you want to do with that data. – abhishekkannojia Oct 06 '21 at 07:27
  • 2
    This seems to be an X/Y problem – mplungjan Oct 06 '21 at 07:46

1 Answers1

-1

for the first request you can do this:

let choiceContext = [];
let nameList = [];
const data = {
    resource_id: '9329755c-6406-4894-88b7-e7b04ccdfecc',
    limit: 10,
};

let succesFunctionSendIN = function sendIN(data){
            $.each(data.result.records, function(recordKey, recordValue) {
                //some code about addding data to choiceContext
                return choiceContext; //assume a result
            });
        };

$.ajax({
    url: 'https://www.data.brisbane.qld.gov.au/data/api/3/action/datastore_search',
    data: data,
    dataType: 'jsonp',
    cache: true,
    success: succesFunctionSendIN
        
});
const answer = succesFunctionSendIN({result[]});

now you have a function reference "succesFunctionSendIN"

second: For store more data You can use HTML web storage.

HTML web storage provides two objects for storing data on the client:

  1. window.localStorage - stores data with no expiration date (max size 5Mb)
  2. window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)Limited only by system memory

Before using web storage, check browser support for localStorage and sessionStorage:

your success function will be like this.

success: function(data) {
   if (typeof(Storage) !== "undefined") {
     localStorage.choiceContext = data.result.records;
   } else {
      // Sorry! No Web Storage support..
   }
 }
Gianluca Musa
  • 755
  • 7
  • 22