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?
3 Answers 3
alertwill implicitly calltoStringonresp, 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.parseJSONanymore.If you want to see this Javascript Object, try
alert(JSON.stringify(resp));use
console.log(resp)to view the data inside the object
Comments
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
Comments
res is the entire response if you want to access status then try resp['prova.com'].status
// it will return 0
dataType : 'JSON'in your ajax function as parameter.debugger;statement beforealert(resp.status)and check what is the object structure of response.