我正在尝试在 python 服务器和网络浏览器之间使用 jsonp 交换数据。客户端看起来像这样(使用 jQuery)并且工作得很好:
$(document).ready(function(){
$.getJSON('http://127.0.0.1:8888/url/here?callback=?', {
key: 'value',
otherKey: 'otherValue'
}, function(data){
// Handles the callback when the data returns
// $.each( data.items, function( antwort, item ) {
// $("div.test").text(item.text);
// })
$("div.test").text('Success');
console.log( data );
$("div.test").text(data.antwort);
// $.each(synval.terms, function(tkey, tval){
// $("div.test").text(tval.term);
});
});
在初始 init 行之后,python 服务器套接字到达如下字符串:
GET /url/here?callback=jQuery111306895637936108335_1435907413063&key=value&otherKey=otherValue&_=143
现在我想返回一个这样的字符串('reply'包含'that is a test':
if data.find("jQuery") <> -1:
json_header = data[data.find("jQuery"): data.find("&")]
reply = json_header + "({\"reply\": \"that is a test\"})\n"
conn.send(reply)
对于通信,我现在使用一个简单的套接字。但这并不是很好。我搜索了一个更高级别的解决方案。所以我现在想使用 BaseHTTPServer 并改为子类 do_GET(self) 。但是我不知道如何实现上面的通信。有人可以帮忙吗?