0

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 リクエストなど) を試しましたが、うまくいきません。複数のリクエストや複数の返品は好きではありません。何か案は?

4

1 に答える 1

0

問題は確かに非常に単純でした。Python CGI スクリプトでif tweets == 1: ... else: ...は、FieldStorage からの文字列である「つぶやき」と数値 1 を比較しているため、実行すると常に else 句に移動します。true と評価されることはないため、常に同じ値を返します。答え。

于 2013-03-15T18:40:37.997 に答える