Reputation: 485
Currently, I have a url which renders data in json format.
url:
http://10.0.1.11/render?target=threshold(400,test)&from=-1mins&format=json&jsonp=?
when run in a browser gives me
?([{"target": "test", "datapoints": [[400, 1388755180], [400, 1388755190], [400, 1388755200], [400, 1388755210], [400, 1388755220], [400, 1388755230], [400, 1388755240]]}])
I would need the json result in a variable for further processing. I tried the following
foo
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$.getJSON("http://10.0.1.11/render?target=threshold(400,test)&from=-1mins&format=json&jsonp=?", function(result){
//response data are now in the result variable
alert(result);
});
</script>
I ideally would need:
var test = [{"target": "test", "datapoints": [[400, 1388755180], [400, 1388755190], [400, 1388755200], [400, 1388755210], [400, 1388755220], [400, 1388755230], [400, 1388755240]]}];
Where am i going wrong?
Upvotes: 0
Views: 64
Reputation: 2771
You need to interpret the request as jsonp rather than json. jsonp is like json, but it's json wrapped in a method-call. (see: What is JSONP all about?)
You can use something like:
<script>
function myCallback(json_data){
//do something with json_data!
}
</script>
<script type="text/javascript" src="http://10.0.1.11/render?target=threshold(400,test)&from=-1mins&format=json&jsonp=myCallback"></script>
or
<script>
$(document).ready(function(){
$.ajax({
url: 'http://10.0.1.11/render?target=threshold(400,test)&from=-1mins&format=json',
dataType: 'jsonp',
success: function(json_data){
//do something with json_data!
}
}
});
})
</script>
(examples adapted from the linked post)
Upvotes: 1