前提・実現したいこと
pythonの標準ライブラリhttp.serverを使ってローカルでサーバーを作り、データの通信実験をしてみました。
しかし、cgiは「CGI script exited OK」と出るのに、POST通信ではブラウザには何も返って来ていないことになっていて困惑しています。
これはコードのミスでした(javascriptで受け取るコードを実行していなかった)のですが、結局受信した結果で例外が発生し、その例外の内容もundefinedになってしまい原因がわかりません。
サーバーに対する知識は全くないので、ネットのありとあらゆる場所からコードを引っ張って作ったコードですが、問題点を教えてください。
発生している問題・エラーメッセージ
cgiは動作するのに値がブラウザで確認できない。(確認として、POST通信ではない状態で、アクセスしたらhtmlを返すコードを追加しましたところ、「Content-Type:」だけが表示されました。※単純なhtmlのミスでした。スイマセン。修正しました)
該当のソースコード
cgi
python
1#!/usr/bin/python3 2# -*- coding: utf-8 -*- 3 4import io 5import os 6import sys 7import cgi 8import cgitb 9import urllib.request 10import json 11 12cgitb.enable() 13 14sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') 15sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 16sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') 17 18 19html = ''' 20<!DOCTYPE html> 21<html lang="ja"> 22<head> 23<meta charset="utf-8"> 24<style> 25body { background-color: cyan; } 26</style> 27<title>初めてのpython3CGI</title> 28<meta name="description" content=""> 29<meta name="keywords" content=""> 30</head> 31<body> 32<header> 33<h1 id="titlename">初めてのpython3CGI</h1> 34</header> 35<div id="contents"> 36<article> 37<section id="top" style="display:block"> 38</section> 39</article> 40</div> 41<footer> 42</footer> 43</body> 44</html> 45''' 46 47print("Access-Control-Allow-Methods: GET ,POST ,OPTIONS") 48print("Access-Control-Allow-Origin: *") 49print("Access-Control-Allow-Headers: *") 50 51if os.environ['REQUEST_METHOD'] == 'POST': 52 form = cgi.FieldStorage() 53 result_json = {'key': 'value'} 54 print("Content-Type: application/json; charset=utf-8\r\n") 55 print('\n') 56 print(json.dumps(result_json)) 57else: 58 print("Content-Type: text/html; charset=utf-8\r\n") 59 print('\n') 60 print(html) 61 62
html
1<!DOCTYPE html> 2<html lang="ja"> 3 4<head> 5 <meta charset="UTF-8"> 6 <title>Javascript試験用</title> 7 <script charset="UTF-8" type="text/javascript" src="ajax.js"></script> 8</head> 9 10<body> 11 <div id="outputArea"></div> 12 <div id="inputArea"> 13 <form> 14 <textarea name="input" rows="2" cols="20"></textarea> 15 </form> 16 <button id="ajaxButton" type="button">決定</button> 17 </div> 18 <div id="dispArea"></div> 19</body> 20 21</html>
javascript
1 2ajaxSend = function (json) { 3 console.log("a") 4 var xhr = new XMLHttpRequest(); 5 xhr.open("POST", 'http://localhost:8000/cgi-bin/server.py', true); 6 xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); 7 xhr.send(json); 8 ajaxGet(); 9} 10 11ajaxGet = function () { 12 var req = new XMLHttpRequest(); 13 try{ 14 if (httpRequest.readyState === XMLHttpRequest.DONE) { 15 if (httpRequest.status === 200) { 16 console.log(req.responseText) 17 alert(req.responseText); 18 }else { 19 alert('リクエストに問題が発生しました'); 20 } 21 } 22 } 23 catch(e){ 24 alert('例外を補足:'+e.description); 25 } 26} 27 28json = { 29 "messages": 30 [ 31 { 32 "date": "2012/1/12 19:12", 33 "text": "こんばんは" 34 } 35 , 36 { 37 "date": "2012/1/11 19:12", 38 "text": "Hello" 39 } 40 ] 41} 42 43window.onload = function(){ 44 document.getElementById("ajaxButton").addEventListener('click',function(){ajaxSend(json)}) 45}
補足情報(FW/ツールのバージョンなど)
使用しているpythonはpython3.7、ブラウザはfirefox、実行環境はwondows10homeです。
回答1件
あなたの回答
tips
プレビュー