0
我正在实现一个系统,通过CGI Python服务器端脚本与网页(HTML + Javascript)进行通信。一个本页面发出请求:Python CGI以不同的方式回应相同的jQuery/AJAX请求
function doQuery(tweets) {
$.getJSON("http://linuxproj.ecs.soton.ac.uk/~onme1g10/watchitlater/cgi-bin/engine.cgi?loading="+ uid +"&tweets="+ tweets, function(data) {
}
)
.success(function(data) {
//alert("complete: " + data['test']);
if (tweets == 1) {
//console.log('tweets is one')
tweetsData = data;
length = Object.keys(tweetsData["tweets"]).length;
intervalVar = window.setInterval(showTweets, 1000);
intervalVar2 = window.setInterval(queryState, 1500);
//console.log("set intervals");
}
else {
console.log('HERE: ' + data["correct"])
queryData = data;
// Function to update state variables
}
})
.error(function(xhr, textStatus, error) {
//alert("error:" + textStatus);
console.log(xhr);
console.log(textStatus);
console.log(error);
})
.complete(function() {/*alert("complete!");*/ });
}
此请求要么?tweets=1
或,这种想法是服务器必须返回取决于值不同的JSON对象。在我的服务器上:
if fs.has_key('state'):
createStateFile()
elif fs.has_key('loading'):
# Return JSON objects
print "Content-type: application/json"
print
option = fs['tweets'].value
if option == 0:
response = {'correct':'1'}
print(json.JSONEncoder().encode(response))
else:
response = harvestTweets()
print(json.JSONEncoder().encode(response))
else:
# Return main page
print "Content-type: text/html"
print
main()
这是行不通的。 else子句(其中option == 1)执行正常,但另一个选项返回未指定的对象。我尝试了很多方法(两个不同的AJAX请求等),但它不起作用。它不喜欢多个请求或多个返回。有任何想法吗?
那么'harvestTweets()'返回什么? – 2013-03-13 19:49:55
返回: tweets = json.load(myFile) 其中myFile是jason对象文件。 – 2013-03-13 19:51:43
因此,1.为什么你不只是读取并返回该文件而不是解码然后重新编码JSON,2.为什么你使用'json.JSONEncoder()。encode'而不是'json.dumps' ? – 2013-03-13 19:54:43