麻烦的JavaScript从Python脚本通过$ .getJSON检索JSON [英] Trouble retrieving JSON from python script via $.getJSON in javascript

查看:157
本文介绍了麻烦的JavaScript从Python脚本通过$ .getJSON检索JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我试图从education.com API访问数据。但是,我仍然无法如愿。

基本上,从我的理解,我应该使用Python脚本燮preSS跨域限制的浏览器。 Python脚本被称为getData.py,而且我用下面的code。逐字:

 #!的/ usr / bin中/蟒蛇对于CGI处理#导入模块
进口CGI,cgitb
进口的urllib2#创建的FieldStorage实例
形式= cgi.FieldStorage()从请求参数URL#下载数据
打印内容类型:文本/ XML \\ r \\ n \\ r \\ n
URL = form.getvalue(URL)
回调= form.getvalue(回调)
REQ = urllib2.Request(URL)
响应= urllib2.urlopen(REQ)
数据= response.read()
打印回调+(+资料+)

然后,我需要通过$ .getJSON调用python脚本在我的JavaScript / jQuery的code。我的教授说我需要通过教育API的网址,呼叫回到这个脚本。我不能确定我将如何做到这一点。我会怎么做呢?什么是我的回调?这是我的jQuery code。我删除从URL的隐私我的钥匙。它是由字改为的myKey

  $的getJSON(getData.py,网址:{url:http://api.education.com/service/service.php?
F = schoolSearch&放大器;关键=&的myKey放大器; SN =港协暨V = 4和城市亚特兰大=&放大器;状态= GA&安培;再
SF = JSON},功能(数据){
的console.log(数据);
});
});


解决方案

序列化检索的数据为JSON。

 #!的/ usr / bin中/蟒蛇对于CGI处理#导入模块
进口CGI,cgitb
进口的urllib2
进口JSON#创建的FieldStorage实例
形式= cgi.FieldStorage()从请求参数URL#下载数据
打印内容类型:文本/ JavaScript的\\ r \\ n \\ r \\ n
URL = form.getvalue(URL)
回调= form.getvalue(回调)
REQ = urllib2.Request(URL)
响应= urllib2.urlopen(REQ)
尝试:
    数据= response.read()
    打印回调+(+ json.dumps(数据)+)
最后:
    response.close()

包含回调=?的URL。 ( http://api.jquery.com/jQuery.getJSON/

 < HTML和GT;
    < HEAD>
        &LT;脚本的src =// ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\"></script>
        &LT;脚本类型=文/ JavaScript的&GT;
            VAR jsonp_url =cgi-bin目录/ getData.py
            VAR URL = \"http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json\";
            $ .getJSON(jsonp_url +'?回调=?',{
                URL:
            },功能(数据){
                的console.log(数据);
                $(身体)文本(数据)。
            });
        &LT; / SCRIPT&GT;
    &LT; /头&GT;    &LT;身体GT;
    &LT; /身体GT;
&LT; HTML和GT;

Right now, I'm trying to access data from the education.com API. However, I'm still unable to do so.

Basically, from what I understand, I'm supposed to use this python script to suppress the cross domain restriction for browsers. The python script is called getData.py, and I'm using the following code. Verbatim:

#!/usr/bin/python 

# Import modules for CGI handling 
import cgi, cgitb 
import urllib2 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

#download data from request parameter 'url' 
print "Content-type:text/xml\r\n\r\n" 
url = form.getvalue("url") 
callback = form.getvalue("callback")
req = urllib2.Request(url) 
response = urllib2.urlopen(req) 
data = response.read() 
print callback + "(" + data+ ")"

Then, I need to call the python script in my JavaScript/jQuery code through the $.getJSON. My professor said I need to pass the url of education API, and the call back to this script. I'm unsure as to how I would do this. How would I do this? What is my callback? This is my jquery code. I removed my key from the url for privacy. It is replaced by the word mykey.

$.getJSON("getData.py", { url: "http://api.education.com/service/service.php?
f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Re
sf=json"}, function(data) {  
console.log(data);
});
});

解决方案

Serialize the retrieved data as json.

#!/usr/bin/python 

# Import modules for CGI handling 
import cgi, cgitb 
import urllib2 
import json

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

#download data from request parameter 'url' 
print "Content-type:text/javascript\r\n\r\n" 
url = form.getvalue("url") 
callback = form.getvalue("callback")
req = urllib2.Request(url) 
response = urllib2.urlopen(req) 
try:
    data = response.read() 
    print callback + "(" + json.dumps(data)+ ")"
finally:
    response.close()

Include callback=? in url. (http://api.jquery.com/jQuery.getJSON/)

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script type="text/javascript">
            var jsonp_url = "cgi-bin/getData.py";
            var url = "http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json";
            $.getJSON(jsonp_url + '?callback=?', {
                url: url
            }, function(data) {
                console.log(data);
                $('body').text(data);
            });
        </script>
    </head>

    <body>
    </body>
<html>

这篇关于麻烦的JavaScript从Python脚本通过$ .getJSON检索JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆