0

Ok. So I've finally figured out how to get a JSON Get request, but I'm having a hard time figuring out how to use the information I get back. I've got 9 items in the array, but I only care about 1 of the values. I'm trying to get the id: value and store it in a variable to use later. I know I currently have no attempt at doing the storing bit in the code - I've tried so many options, I reverted the code back to where it would at least get the array.

Here is the ajax code:

 var childSection;
        var url = "https://myurl.com";
        $.ajax({
    'async': true,
    'type': "GET",
    'global': false,
    'dataType': 'JSON',
    'data': JSON.stringify(childSection),
    'contentType': "application/json",
    'url': url,
    'success': function (data) {
        childSection = data;
        return childSection;  
    }
3
  • Picture doesnt work, link is broken. Commented Mar 2, 2017 at 21:43
  • 1
    Assuming the picture is of the data object, I think childSection = data[0].id should do the trick. Commented Mar 2, 2017 at 21:45
  • Yes, it's just a picture of the array, but I can't upload pics yet :P I'll try your suggestion. Commented Mar 2, 2017 at 21:47

1 Answer 1

1

Inside the "success" property, you have a function which is receiving the data. You can take data and access the node that you want to save. Let's say the node in question is called wutangclan, you can access it like so: data.wutangclan.

If you want to be able to get to data.wutangclan outside of the success function, there are several ways, but basically you want to assign it to a variable that was created outside of the success function.

Also, another way to do this is like so:

// since I defined this in the global scope, I can access it later inside the done function
var myGlobalVariable;

var request = $.ajax({
          url: url,
          method: "GET",
          data: {
              yourdata: the_value_to_send
          },
          dataType: "json"
        });

        request.done(function(data) {
          myGlobalVariable = data.wutangclan
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.