Lotus@ 2013-06-18 23:53 采纳率: 100%
浏览 68

python脚本中的回调

I've never used python before. Thus, I'm kind of confused. I'm trying to use python script to obtain data from the education api. I'm unsure as to how I would accomplish this. I have the following python script:

#!/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

"  
url = form.getvalue("url") 
callback = form.getvalue("callback")
req = urllib2.Request(url) 
response = urllib2.urlopen(req) 
data = response.read() 
print callback + "(" + data+ ")"

I know I would replace the url in this text with url of the website I'm requesting the data from. However, what do I replace callback with?

I also understand I will need make a ajax call to retrieve the json data in my browser. How would I do this?

The code I have to return the json is the following:

$.ajax({
url : "getData.py",
data : {url : "http://api.education.com/service/service.php?f=schoolSearch&key=mykey&
sn=sf&v=4&city=Atlanta&state=ga&Resf=json"},
complete : function(xhr, result) {
if (result != "success")
return;
var response = xhr;
console.log(var);
  • 写回答

1条回答 默认 最新

  • weixin_33726318 2013-06-19 00:13
    关注

    This looks like a reference to the JSONP syntax commonly in use to bypass cross domain security restriction that are part of the browser. You can think of this as an interaction in the following format:

    standard request:
      url => json payload
    
    jsonp request:
      url?callback=something => something(json_payload)
    

    jQuery will normally fill the callback parameter in for you if you pass the

    dataType: "jsonp"
    

    flag to your ajax request as noted in this documentation: http://api.jquery.com/jQuery.ajax/

    so the answer to your question about the callback parameter is "whatever method you want to handle the response". This is strictly used in the JavaScript environment to make the request across domains without the benefit of an intermediary. If you are using a Python proxy to make this request (which is appears you may be able to do) you can just leave the callback parameter off to retrieve the standard json data payload and return that value to the requesting client.

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?