目标是让基于 web.py 的 Python 网页服务通过 JSONP 将一个列表对象发送回 jQuery,以便 jQuery 可以通过 AJAX 调用访问该服务。
- 尽管阅读了很多示例,但仍然不知道如何让它起作用。
- 以下是正在使用的代码:
- JavaScript:
- JavaScript:
xmlrpcproxy = 'http://0.0.0.0:1885/'; //Back To The Future III reference on the port :)
jQuery.ajaxSetup ({
url: xmlrpcproxy, // <--- returns valid json if accessed in the browser
type: "GET",
cache: false,
contentType: "jsonp", // Pay attention to the dataType/contentType
dataType: 'jsonp', // Pay attention to the dataType/contentType
});
jQuery.ajax({
success: function(data) {
console.log("You made it!");
},
error: function (xhr) {
console.log("Error: " + xhr.statusText);
}
}).done(function(data){
console.log(data);
var firstoption = 'Please Select';
jQuery("select#ItemIDSelect").html(firstoption);
var i;
var erplist = JSON.parse(data);
jQuery("responsearea").append(erplist);
for (i = 0; i < erplist.length; ++i) {
jQuery("select#ItemIDSelect").append('' + erplist[i] + '');
}
});
* **Python web.py 代码:**
import web
import xmlrpclib
import json
urls = ( '/(.+)', 'index', )
class index: def GET(self): #THE FOLLOWING IS A SUCCESFULL QUERY FOR DATA TO AN ERP SERVER server = '..*.**' #hidden the openerp server for stackoverflow post username = 'admin' #the user pwd = 'admin' #the password of the user dbname = 'demo' #the database
# Get the uid
sock_common = xmlrpclib.ServerProxy ('http://%s:8069/xmlrpc/common'%(server))
uid = sock_common.login(dbname, username, pwd)
#replace localhost with the address of the server
sock = xmlrpclib.ServerProxy('http://%s:8069/xmlrpc/object'%(server))
# Unactive all product with a stock = 0.0 and using the ancient code
ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', [])
p_ids = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, ['name'])
#END OF THAT PARTICULAR QUERY
a=[]
for p in p_ids:
a.append(p['name'])
b = json.dumps(a)
return 'some_function(' + b + ')
example: typical content of b ["The Jackson Group's Project", "Research & Development", "E-Learning Integration", "Website Design Templates", "Data Import/Export Plugin", "1", "Project XXX", "Contract Agrolait", "Project : Agrolait"] ```
-
解决方案
- jQuery 似乎在回调查询参数中提供了回调函数名称。
- 确保设置了正确的内容类型标头。
- 回调函数名称可以在 Python 脚本中使用
web.input(callback='callback')获取。 - 以下是如何修改 Python 脚本以返回 JSONP 回调的示例:
import web import xmlrpclib import json
urls = ( '/(.+)', 'index', )
class index: def GET(self): callback_name = web.input(callback='callback').callback web.header('Content-Type', 'application/javascript')
#THE FOLLOWING IS A SUCCESFULL QUERY FOR DATA TO AN ERP SERVER
server = '***.***.*.**' #hidden the openerp server for stackoverflow post
username = 'admin' #the user
pwd = 'admin' #the password of the user
dbname = 'demo' #the database
# Get the uid
sock_common = xmlrpclib.ServerProxy ('http://%s:8069/xmlrpc/common'%(server))
uid = sock_common.login(dbname, username, pwd)
#replace localhost with the address of the server
sock = xmlrpclib.ServerProxy('http://%s:8069/xmlrpc/object'%(server))
# Unactive all product with a stock = 0.0 and using the ancient code
ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', [])
p_ids = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, ['name'])
#END OF THAT PARTICULAR QUERY
a=[]
for p in p_ids:
a.append(p['name'])
b = json.dumps(a)
return '%s(%s)' % (callback_name, b)
example: typical content of b ["The Jackson Group's Project", "Research & Development", "E-Learning Integration", "Website Design Templates", "Data Import/Export Plugin", "1", "Project XXX", "Contract Agrolait", "Project : Agrolait"] ```
- 现在,jQuery 应该能够正确地处理 JSONP 响应。