実現したいこと
HTMLで作成したページから簡単に単位変換を行うことのできる仕組みを、AJAX/CGI(Python)で作りたいと考えています。
前提
現在、下記のようなプログラムのプロトタイプを作成しています。
- HTMLのform要素で入力された(プロトタイプでは自動設定)変換前数値・変換前単位・変換後単位をAJAXからPythonファイルへ渡す
- 渡された値を基にpintライブラリを用いて単位変換を実行、結果を出力する
- 出力された結果をAJAXで受け取り、指定したP要素内に表示
ただ何故か出力結果が毎回正しく表示されず、困ってしまっています。
※なおAJAXについては初心者なので、見当違いなコードになってしまっていたら申し訳ありません。
発生している問題・エラーメッセージ
ブラウザのコンソール上に問題は出力されていないようです。
P要素の表示は毎回[object Object]となります。
該当のソースコード
index.html
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4<meta charset="UTF-8"> 5<meta name="viewport" content="width=device-width, initial-scale=1.0"> 6<title>Unit Converter Test</title> 7<script src="jquery-3.6.0.min.js"></script> 8</head> 9<body> 10<main> 11<h1>Unit Converter</h1> 12<br> 13<form id="converter" name="converter" method="post"> 14<label for="num">変換前の数値を入力:</label> 15<input type="number" id="num" name="num"> 16<br> 17<label for="before">変換前の単位を選択:</label> 18<input type="text" id="before" name="before" value="cm"> 19<br> 20<label for="after">返還後の単位を選択:</label> 21<input type="text" id="after" name="after" value="in"> 22<br> 23<input type="button" id="submit_btn" value="送信" onclick="post_with_ajax()"> 24</form> 25<p id="result-area"></p> 26 27</main> 28<script> 29function post_with_ajax() { 30let formData = $('#converter'); 31$.ajax({ 32type: "post", 33url: './converter.py', 34data: formData.serialize(), 35}).done(function(data){ 36document.getElementById('result-area').textContent = data; 37}).fail(function(data){ 38document.getElementById('result-area').textContent = data; 39}); 40} 41</script> 42</body> 43</html>
converter.py
Python
1#!/usr/local/bin/python3.7 2 3import cgi 4import pint 5 6storage = cgi.FieldStorage() 7print('Status: 200 OK') 8print('Content-Type: text/html\n') 9 10ureg = pint.UnitRegistry() 11 12try: 13 value = float(storage.getvalue('num')) 14 from_unit = storage.getvalue('before') 15 to_unit = storage.getvalue('after') 16 17 # Create quantity with unit 18 quantity = value * ureg(from_unit) 19 20 # Perform conversion 21 converted = quantity.to(to_unit) 22 23 # Output result 24 print(f"\n{value} {ureg(from_unit).units:~} = {converted.magnitude:.2f} {converted.units:~}") 25 26except pint.errors.UndefinedUnitError: 27 print("Error: One or both of the units are not recognized.") 28except pint.errors.DimensionalityError: 29 print("Error: The units are not compatible for conversion.") 30except ValueError: 31 print("Error: Invalid numeric input.") 32except Exception as e: 33 print(f"An unexpected error occurred: {e}") 34
試したこと
Pythonコード/ライブラリに問題があるのかと考え、下記のようなテストプログラムを作成しSSH経由で実行しましたが、結果は問題なく出力されているようでした。
Python
1import pint 2 3print('Status: 200 OK') 4print('Content-Type: text/html\n') 5 6ureg = pint.UnitRegistry() 7 8try: 9 value = 10.0 10 from_unit = 'cm' 11 to_unit = 'in' 12 13 # Create quantity with unit 14 quantity = value * ureg(from_unit) 15 16 # Perform conversion 17 converted = quantity.to(to_unit) 18 19 # Output result 20 print(f"\n{value} {ureg(from_unit).units:~} = {converted.magnitude:.2f} {converted.units:~}") 21 22except pint.errors.UndefinedUnitError: 23 print("Error: One or both of the units are not recognized.") 24except pint.errors.DimensionalityError: 25 print("Error: The units are not compatible for conversion.") 26except ValueError: 27 print("Error: Invalid numeric input.") 28except Exception as e: 29 print(f"An unexpected error occurred: {e}") 30input() 31
補足情報(FW/ツールのバージョンなど)
環境はLolipopレンタルサーバ内、Pythonのバージョンは3.7です。
追記(2025/4/23 13:40)
converter.pyを直接ブラウザで開いたところ、Internal Error 500 が出力されてしまいました。
なおPythonの参照先は以前に作成したCGI と同一、パーミッションも700となっているのでこちらの問題とはあまり関係がないかと思います。
以上です。
ご教示いただけますと幸いです。
よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー