I am developing a Desktop widget (in Windows 7). I have created an API (using PHP) which the widget needs to request data from. The API is in JSON format, and by typing the URL in the browser I get the data I need, which is
{"a": "b"}
I also checked the JSON API in http://jsonlint.com which shows that the JSON is correct.
The javascript code I use to get the JSON from the URL is the following :
$.getJSON(url, function(data) {
... printings ...
});
but I don't get any results (whereas the URL shows the correct results when typing it). When I open the Network Tab, I can see that the URL is not even requested! The server request does not return anything, it's like I didn't request the URL.
However, when I type a different URL (not created by me, but from my company, using Python), the widget gets the data. I suppose that there is therefore something wrong with my API, but I cannot figure out what. In the PHP code creating the API I have
<?php
....
$str = 'b';
....
$results = array();
$results['a'] = $str;
$encoded = json_encode($results);
echo $encoded;
return $encoded;
?>
and I get the JSON I show above. Any ideas?
there must be obviously something wrong with the javascript part. and i don't know what you mean by Desktop Widget.
your javascript isn't outputting anything from your JSON response. let's change that by printing data.a
, which should be the variable of your response:
$.getJSON(url, function(data) {
output+= "<p> my JSON response: " + data.a + " </p>";
document.getElementById('demo').innerHTML = output;
//whenDone (output);
});
to be honest, i don't what you are trying to achieve with whenDone(output)
. is this a function written by yourself?
you realize that $.getJSON()
is a jQuery function - so make sure you included jQuery in your code. and also make sure that your script is in <script></script>
tags.