简体   繁体   English

从jsonp获取json数据

[英]obtain json data from jsonp

Currently, I have a url which renders data in json format. 目前,我有一个以json格式呈现数据的url。

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. 我需要将json结果放入变量中以进行进一步处理。 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? 我要去哪里错了?

You need to interpret the request as jsonp rather than json. 您需要将请求解释为jsonp而不是json。 jsonp is like json, but it's json wrapped in a method-call. jsonp类似于json,但它是通过方法调用包装的json。 (see: What is JSONP all about? ) (请参阅: JSONP的全部含义是什么?

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) (示例摘自链接文章)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM