1

I got from an API this response from Ajax CALL:

{"prova.com":{"status":0}}

I try to access to status = 0 using this script:

$.ajax({
 type: 'GET',
 url: 'https://api.cloudns.net/domains/check-available.json?auth-id=1243&auth-password=KNK-dn5.&name=' +dominio + '&tld[]=' + tld,
 success: function(resp) {
 alert(resp.status)
 }
});

But it return undefined. If i use resp it return [Object object] any suggestion?

Komal12
3,3584 gold badges18 silver badges25 bronze badges
asked Jul 21, 2017 at 7:10
3
  • put dataType : 'JSON' in your ajax function as parameter. Commented Jul 21, 2017 at 7:11
  • Are you trying to access the "status" key in the API response or the status of the actual http response? Commented Jul 21, 2017 at 7:13
  • Try putting a debugger; statement before alert(resp.status) and check what is the object structure of response. Commented Jul 21, 2017 at 7:15

3 Answers 3

2
  • alert will implicitly call toString on resp, which will be an object literal (how the JSON is parsed), and that value is "[object Object]"

  • resp is no longer in JSON format, it is a Javascript Object. You do not need to use function like jQuery.parseJSON anymore.

    If you want to see this Javascript Object, try alert(JSON.stringify(resp));

  • use console.log(resp) to view the data inside the object

answered Jul 21, 2017 at 7:21
Sign up to request clarification or add additional context in comments.

Comments

1

your json data

var obj = {"prova.com":{"status":0}}

using ajax response object resp

for(var prop in resp) {
 var item = resp[prop];
 //console.log(item);
 alert(item); 
}

here is a demo code

var obj = {"prova.com":{"status":0}}
for(var prop in obj) {
 var item = obj[prop];
 console.log(item);
 alert(item.status); 
}

try this

answered Jul 21, 2017 at 7:26

Comments

0

res is the entire response if you want to access status then try resp['prova.com'].status

// it will return 0

answered Jul 21, 2017 at 7:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.