I’m having an issue obtaining json from a piece of hardware on a local network.
I can view the json code using this address in a browser:
http://192.168.1.103/device.json
I also saved the resulting text to a file device.json and placed it in the same directory as my test.php file.
This code works:
$.getJSON('device.json',function(data) {
$.each(data.mainunit.sensors, function(i, sensor) {
$("#results").append('<p>'+sensor.name+' = '+sensor.tc+'</p>');
});
This doesn’t work: (changing device.json to http://192.168.1.103/device.json)
$.getJSON('http://192.168.1.103/device.json',function(data) {
$.each(data.mainunit.sensors, function(i, sensor) {
$("#results").append('<p>'+sensor.name+' = '+sensor.tc+'</p>');
});
My guess is the code can’t view the JSON file at the IP address.
Thanks in advance.
You should wrap your JSON with some callback function. If your
device.jsonlooks like that:Modify it to be JSONP-compliant:
How to request:
Didn’t test the code.