0

Im trying to access to parameter value from response.

  $.ajax({
        type: "POST",
        url: "",
        dataType: 'text',
        async: false,
        headers: {
            "Authorization": "Basic " + btoa(username + ":" + password)
        },
        data: '{ "action" : "create", "resource" : {"name" : "teddssssssssddsssdsddddsdddwdsdssdsddi",  "description": "Test Tenant Description","parent": {"href": "localhost"}}}',
        success: function(as) {
            alert(as[0][0]["id"]);          
        }
  });

in the success area i got the response, in json. I want to access to the id value, but i cant. always says undefined, i tried different options. this is the response format.

{
  "results": [
    {
      "href": "localhost/1111111",
      "id": "100000000111",
      "name": "test",
      "ancestry": "1000011111",
      "divisible": true,
      "description": "Test Tenant Description",
      "use_config_for_attributes": false,
      "default_miq_group_id": "10021200000173"
    }
  ]
}

I dont know if i can access direct to the parameter or better get the response as text and split.

2
  • 1
    try with as["results"][0]["id"] Commented Jul 2, 2018 at 22:48
  • 2
    Never ever use async:false. It is a terrible practice and is deprecated by the browser vendors. You should be seeing warning about that in console Commented Jul 2, 2018 at 22:57

1 Answer 1

2

The response has a different structure. Your response is an object with one property and value is an array of objects.

something like {'results': [...{}]}

Key 0 does not exist.

const data = {"results":[{"href":"localhost/1111111","id":"100000000111","name":"test","ancestry":"1000011111","divisible":true,"description":"Test Tenant Description","use_config_for_attributes":false,"default_miq_group_id":"10021200000173"}]};


console.log(data.results[0].id);

Sign up to request clarification or add additional context in comments.

1 Comment

This answer works, i had to change datatype to json. Thanks

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.