質問編集履歴

2

URL

2022/09/08 09:06

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
  fd.append('long', longitude);
35
35
 
36
36
  // フォームの入力値を送信
37
- fetch( 'http://127.0.0.1:8000/', {
37
+ fetch( 'APIのURL', {
38
38
  method: 'POST',
39
39
  body: fd
40
40
  })

1

コード、前提の追加

2022/09/08 09:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,6 @@
1
+ ### 前提
2
+ 現在地から目的地まで、GoogleMapのAPIを使用して経路を検索するサイトを作っています。
3
+
1
4
  ### 実現したいこと
2
5
  JavaScriptで取得した現在地情報をPythonに送りたいです。
3
6
 
@@ -22,5 +25,29 @@
22
25
  function errorCallback(error){
23
26
  alert("位置情報が取得できませんでした");
24
27
  };
28
+
29
+ // FormDataオブジェクトの初期化
30
+ const fd = new FormData();
31
+
32
+ // FormDataオブジェクトにデータをセット
33
+ fd.append('lat', latitude);
34
+ fd.append('long', longitude);
35
+
36
+ // フォームの入力値を送信
37
+ fetch( 'http://127.0.0.1:8000/', {
38
+ method: 'POST',
39
+ body: fd
40
+ })
41
+ .then(response => response.json())
42
+ .then(data => {
43
+ console.log(data);
44
+ })
45
+ .catch((error) => {
46
+ console.error(error);
47
+ });
25
48
  ```
26
49
 
50
+ こちらのサイトを参考にしたのですが、いまいち理解できていません。
51
+ https://gray-code.com/javascript/sending-form-data-by-fetch-api/
52
+
53
+ Pythonの方ではどのように受け取ったら良いのでしょうか。