0

JQuery を使用して Python スクリプトを呼び出す単純な Web アプリケーションを作成しようとしています。Python スクリプトは JSON 形式でデータを返します。私のJavascriptでのajax呼び出しは次のとおりです。

console.log('before');

$.ajax({
    dataType: "json",
    url: "simple.py",
    success: function() { alert("Success"); },
    error: function(request, error) { console.log(request); }
});

console.log('after');

これが私のpythonスクリプトです:

#!/usr/bin/env python
import json

l = "hello"
print json.dumps(l)

Javascript を実行すると、「内部サーバー エラー」が発生します。ajax 呼び出しで dataType を「script」に変更printしたり、 return.

代わりに、オンラインの例から取得した次のようなプレーンな JSON ファイルを使用すると、次のようになります。

{
  "one": "Singular sensation",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

dataType: "json"url: "test.json"それは正常に動作します。JSON の代わりに一連の HTML を出力する Python スクリプトを使用すると、それも問題なく動作します。コマンドラインから simple.py を単独で実行すると、何も問題がなかったかのように結果が出力されます。ただし、何らかの理由で、私の JQuery は JSON データを受信しません。Web Inspector の「Network」の下に、simple.py のタイプが text/html としてリストされていますが、これは間違っているようです。これはおそらく application/json である必要があると思いますが、変更方法がわかりません。

私は Mac OSx に付属の Apache2 サーバーを使用しており、すべての Web サイト ファイルを Library/WebServer/Documents の下に配置しています。「localhost」URL から自分の Web サイトにアクセスしています。

ログのエラー:

(8)Exec format error: exec of '/Library/WebServer/Documents/UBCNetworks/simple.py' failed, referer: localhost/UBCNetworks/index.html
 .... Premature end of script headers: simple.py, referer: localhost/UBCNetworks/index.html 
4

1 に答える 1