I have a small script that gets some JSONP data from the server. I want to access various values in that response data. I don't know how to do it.
After reading some articles I am confused. I read from this website that JSON.stringify()
will create JSON. So it means the server is not returning JSON in my case?
Because if I do data= JSON.parse(response);
the alert()
doesn't work. Also if I change the dataType
to json I get some authentication error from server. After all this I finally figured out how to alert the server's response.
jQuery code:
getData = function(){
var urlink = "https://192.168.150.3/loc/102?jsonpCallback=myCallback";
$.ajax({
type: "GET",
url: urlink,
dataType:"jsonp",
async: false,
});
}
myCallback = function(response){
data= JSON.stringify(response);
alert(data);
}
The output I get from above script due to alert is like this:
{
"request":"/loc/102?jsonpCallback=myCallback&callback=jQuery21106404822329059243_1410515165630&_=1410515165633",
"response":
{
"id":102,
"name":"Location 1",
"child":[
"\/child\/CSJ01",
"\/child\/CSJ02",
],
"stats":{
"pow":{
"instant":8.39
},
"cTemp":{
"instant":22.76
},
"rTemp":{
},
"b":{
"instant":1
},
"m":{
"instant":1410513940
}
}
}
}
Question: How can I access the values of id
, name
, child
, the value of instant
in pow
, the value of instant
in cTemp
etc. ?
I have found some solutions about how to get these values in JSON but they didn't work for me. For eg: This example. In most of the examples they first use JSON.parse()
and then get the specific values, but in my case JSON.parse()
does nothing, so these examples didn't help a lot OR maybe I am extremely confused and lost. Any suggestions will be appreciated.