Skip to content

Commit 987e2f7

Browse files
committed
サンプルの追加と説明の追加
1 parent 344d30b commit 987e2f7

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

server/api_server.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ def do_GET(self):
4444
api_type = query.get('api', ['users-utf8'])[0]
4545

4646
if api_type == 'users-sjis':
47-
# Shift-JISでレスポンス
47+
# Shift-JISでレスポンス(適切なヘッダー付き)
4848
self.send_response(200)
4949
self.send_header('Content-type', 'application/json; charset=Shift_JIS')
5050
self.send_header('Access-Control-Allow-Origin', '*') # CORS
5151
self.end_headers()
5252

53+
json_utf8 = json.dumps(users, ensure_ascii=False)
54+
json_sjis = json_utf8.encode('cp932')
55+
self.wfile.write(json_sjis)
56+
elif api_type == 'users-sjis-no-header':
57+
# Shift-JISでレスポンス(不適切なヘッダー - overrideMimeTypeテスト用)
58+
self.send_response(200)
59+
self.send_header('Content-type', 'application/json') # charsetなし
60+
self.send_header('Access-Control-Allow-Origin', '*') # CORS
61+
self.end_headers()
62+
5363
json_utf8 = json.dumps(users, ensure_ascii=False)
5464
json_sjis = json_utf8.encode('cp932')
5565
self.wfile.write(json_sjis)

src/js/main.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ document.addEventListener('DOMContentLoaded', () => {
5858
.then(users => render('sjis-data-fetch', createTable(users)))
5959
.catch(error => renderError('sjis-data-fetch', error));
6060

61-
// --- 2. XMLHttpRequest ---
61+
// --- 2. XMLHttpRequest (without overrideMimeType) ---
6262
const xhr = new XMLHttpRequest();
6363
xhr.open('GET', '/api?api=users-sjis');
6464
xhr.onload = () => {
@@ -76,6 +76,43 @@ document.addEventListener('DOMContentLoaded', () => {
7676
xhr.onerror = () => renderError('sjis-data-xhr', 'Request error');
7777
xhr.send();
7878

79+
// --- 2b. XMLHttpRequest (without overrideMimeType, no charset header) ---
80+
const xhr2 = new XMLHttpRequest();
81+
xhr2.open('GET', '/api?api=users-sjis-no-header');
82+
xhr2.onload = () => {
83+
if (xhr2.status >= 200 && xhr2.status < 300) {
84+
try {
85+
const users = JSON.parse(xhr2.responseText);
86+
render('sjis-data-xhr-no-override', createTable(users));
87+
} catch (e) {
88+
renderError('sjis-data-xhr-no-override', e);
89+
}
90+
} else {
91+
renderError('sjis-data-xhr-no-override', `Request failed: ${xhr2.status}`);
92+
}
93+
};
94+
xhr2.onerror = () => renderError('sjis-data-xhr-no-override', 'Request error');
95+
xhr2.send();
96+
97+
// --- 2c. XMLHttpRequest (with overrideMimeType) ---
98+
const xhr3 = new XMLHttpRequest();
99+
xhr3.open('GET', '/api?api=users-sjis-no-header');
100+
xhr3.overrideMimeType('text/plain; charset=Shift_JIS');
101+
xhr3.onload = () => {
102+
if (xhr3.status >= 200 && xhr3.status < 300) {
103+
try {
104+
const users = JSON.parse(xhr3.responseText);
105+
render('sjis-data-xhr-override', createTable(users));
106+
} catch (e) {
107+
renderError('sjis-data-xhr-override', e);
108+
}
109+
} else {
110+
renderError('sjis-data-xhr-override', `Request failed: ${xhr3.status}`);
111+
}
112+
};
113+
xhr3.onerror = () => renderError('sjis-data-xhr-override', 'Request error');
114+
xhr3.send();
115+
79116
// --- 3. axios ---
80117
// axiosはグローバルに読み込まれている前提
81118
if (window.axios) {

src/templates/index.ejs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,37 @@
99
<h1>Shift-JIS Web サンプル</h1>
1010

1111
<h2>API (Shift-JIS) - fetch + TextDecoder</h2>
12+
<p><small>API: Shift-JISレスポンス + 適切なcharsetヘッダー<br>フロント: fetchでarrayBuffer取得 → TextDecoderで明示的にShift-JISデコード</small></p>
1213
<div id="sjis-data-fetch">
1314
<p>ロード中...</p>
1415
</div>
1516

16-
<h2>API (Shift-JIS) - XMLHttpRequest</h2>
17+
<h2>API (Shift-JIS) - XMLHttpRequest (without overrideMimeType)</h2>
18+
<p><small>API: Shift-JISレスポンス + 適切なcharsetヘッダー<br>フロント: XMLHttpRequest、ブラウザによる自動デコードに依存</small></p>
1719
<div id="sjis-data-xhr">
1820
<p>ロード中...</p>
1921
</div>
2022

23+
<h2>API (Shift-JIS) - XMLHttpRequest (without overrideMimeType, no charset header)</h2>
24+
<p><small>API: Shift-JISレスポンス + charsetヘッダーなし<br>フロント: XMLHttpRequest、特別な処理なし → 文字化けのはず</small></p>
25+
<div id="sjis-data-xhr-no-override">
26+
<p>ロード中...</p>
27+
</div>
28+
29+
<h2>API (Shift-JIS) - XMLHttpRequest (with overrideMimeType)</h2>
30+
<p><small>API: Shift-JISレスポンス + charsetヘッダーなし<br>フロント: XMLHttpRequest + overrideMimeType()で明示的にcharset指定</small></p>
31+
<div id="sjis-data-xhr-override">
32+
<p>ロード中...</p>
33+
</div>
34+
2135
<h2>API (Shift-JIS) - axios</h2>
36+
<p><small>API: Shift-JISレスポンス + 適切なcharsetヘッダー<br>フロント: axios、ブラウザによる自動デコードに依存</small></p>
2237
<div id="sjis-data-axios">
2338
<p>ロード中...</p>
2439
</div>
2540

2641
<h2>API (UTF-8) - fetch</h2>
42+
<p><small>API: UTF-8レスポンス + UTF-8ヘッダー<br>フロント: fetch + response.json()、通常の処理</small></p>
2743
<div id="utf8-data">
2844
<p>ロード中...</p>
2945
</div>

0 commit comments

Comments
 (0)