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

  • SEARCH
  • Home
  • 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 6793041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:11+00:00 2026-05-26T18:03:11+00:00

How can I assign a variable(var) to a json object which is returned from

  • 0

How can I assign a variable(var) to a json object which is returned from ajax call to the server? I need to access that object in the rest of the body.

For example, I’ve try this, I don’t know it’s correct.

var selectValues=$(document).ready(function() {
    $.ajax({
                  type: "POST",
                  url: "http://10.0.2.2/mobileajax/callajax.php",
                  data: ({name: theName}),
                  cache: false,
                  dataType: "text",
                  success: onSuccess
                });
})


var $vendor = $('select.mobile-vendor');
var $model = $('select.model');
$vendor.change(
              function() {
                  $model.empty().append(function() {
                      var output = '';
                      $.each(selectValues[$vendor.val()], function(key, value) {
                          output += '<option>' + key + '</option>';
                      });
                      return output;
                  });
              }).change();

// bonus: how to access the download link
$model.change(function() {
    $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
});

Note that, variable selectValues is used in the rest of the body.

  • 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-05-26T18:03:12+00:00Added an answer on May 26, 2026 at 6:03 pm

    I think you should do the whole code inside the $(document).ready(function() function and then make the ajax call synchronous. Because now the ajax call will be made when the document is ready, but the other code will be run directly without waiting for the document to be ready. Also the ajax call is asynchronous by default, so you should make it synchronous and assign the selectValues variable inside the success function of the ajax call. It will become something like this:

    $(document).ready(function() {
            var selectValues;
            $.ajax({
                  type: "POST",
                  url: "http://10.0.2.2/mobileajax/callajax.php",
                  data: ({name: theName}),
                  cache: false,
                  dataType: "text",
                  async: false,
                  success: function(data) {
                    selectValues = data
                  }
                });
    
                var $vendor = $('select.mobile-vendor');
                var $model = $('select.model');
                $vendor.change(
                              function() {
                                  $model.empty().append(function() {
                                      var output = '';
                                      $.each(selectValues[$vendor.val()], function(key, value) {
                                          output += '<option>' + key + '</option>';
                                      });
                                      return output;
                                  });
                              }).change();
    
                // bonus: how to access the download link
                $model.change(function() {
                    $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
                });
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to assign a variable to returned JSON data that has been manipulated with
Question: How can I access a member variable in assembly from within a non-POD
How can i assign value to javasctipt variable from code-behind (C#)? <script type=text/javascript> String.prototype.trim
can we assign null value to struct type of variable? struct MyStruct { }
In ANSI C++, how can I assign the cout stream to a variable name?
How can we re assign the value of a StringBuffer or StringBuilder Variable? StringBuffer
I can assign a tuple as follows: var (min, max) = (1, 2) But
Is there a simple attribute or data contract that I can assign to a
Is it possible to do template layouts which you can assign to all your
In order to return a value from a VB.NET function one can assign a

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.