Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:32:38+00:00 2026-06-13T13:32:38+00:00

Hi I need to store an AJAX Response into two variables x and y

  • 0

Hi I need to store an AJAX Response into two variables x and y or into a array.
My AJAX response is a array. I am able to see the data but only with and alert into che call.
I need the data outside the ajax call

var x;
var y;

$.ajax({
    url: 'ajaxload.php',
    dataType: "json",
    success: function (data) {
        x = data.posX;
        y = data.posX;
        alert(x + " " + y);  // I can se data but I need outside ajax call
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:32:38+00:00Added an answer on June 13, 2026 at 1:32 pm

    If I understand correctly, you want to reuse the ajax response later within your code.
    If that’s the case, your current code wouldn’t work because by default, the javascript engine doesn’t wait for the response of ajax requests. In other words the code below won’t work:

    <script type="text/javascript">
    $(document).ready(function(){
        var x; 
        var y;
        $.ajax({
            url: 'ajaxload.php',
            dataType: "json", 
            success: function(data) { 
                x= data.posX;
                y= data.posX;
                alert (x+" "+y);  // I can se data but I need outside ajax call
            }
        });
        alert(x+" "+y); // You won't see anything, because this data isn't yet populated. The reason for this is, the "success" function is called when the ajax request has finished (it has received a response).
    })
    </script>
    

    You need to wait for the ajax response. To do that with jQuery you need to slightly modify your code:

    <script type="text/javascript">
    $(document).ready(function(){
        var data = $.parseJSON($.ajax({
            url:  'ajaxload.php',
            dataType: "json", 
            async: false
        }).responseText); // This will wait until you get a response from the ajax request.
    
        // Now you can use data.posX, data.posY later in your code and it will work.
        var x = data.posX;
        var y = data.posY;
        alert(x+" "+y);
        alert(data.posX+" "+data.posY);
    });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with jQuery's Ajax but in my case I need to store the
After two days of struggling, I've made my own Ext.data.Store class that works (see
I need to set an ajax request with a generated url. Ext.define('Cc.store.Absences', { extend:
I need to pass the response from ajax call to a jquery template.The response
I'm loading wordpress pages dynamically using ajax. It fires properly but I need to
I need store just 10 arrays in my app, which I can change from
I have id values for products that I need store. Right now they are
I need to store a very large amount of instances of my class, and
I need to store a login session when the user is login and remove
I need to store GPS coordinates in a database. I've heard that floats are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.