Web ページ (HTML + Javascript) が CGI Python サーバー側スクリプトを介して通信するシステムを実装しています。このページの 1 つが要求を行います。
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
か?tweets=0
が含まれており、サーバーはその値に応じて異なる 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 句 (オプション == 1) は正常に実行されますが、他のオプションは未指定のオブジェクトを返します。さまざまな方法 (2 つの異なる AJAX リクエストなど) を試しましたが、うまくいきません。複数のリクエストや複数の返品は好きではありません。何か案は?