質問編集履歴

2

別のオリジンからJSONを書き換えるレスポンスが欲しいです

2023/09/22 02:21

投稿

irebasunglass
irebasunglass

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ###
2
- CORSを潜り抜けてlocalhost:3000(React)からxammpのphpファイルたい
2
+ CORSを潜り抜けてlocalhost:3000(React)からxammpのphpにリクエストをし
3
-
3
+ クライアント側にあるJSONの中身を書き換えたい
4
4
  ###
5
5
 
6
6
  CORSを潜り抜けてlocalhost:3000(React)からxammpのphp(localhost:80)にリクエストをするphpとき![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-09-21/95a54987-4aa9-4185-82b9-bcf2d6d0f777.png)

1

CORSをくぐったと思われていましたがくぐれていなかったのでコードの変更とより具体的な詳細を載せております

2023/09/21 06:28

投稿

irebasunglass
irebasunglass

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,39 +1,61 @@
1
1
  ###
2
- CORSを潜り抜けてlocalhost:3000(React)からxammpのphpファイルにリクエストするphpファイルを作りたい
2
+ CORSを潜り抜けてlocalhost:3000(React)からxammpのphpファイルに渡したい
3
3
 
4
- ### 前提
4
+ ###
5
5
 
6
- xamppのほかのファイルはphpは作動しますがどうしてもCORS対策をした
7
- header("Access=Control-Allow-Origin: http://localhost:3000/");
6
+ CORSを潜り抜けてlocalhost:3000(React)からxammpのphp(localhost:80)にリクエストをするphpとき![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-09-21/95a54987-4aa9-4185-82b9-bcf2d6d0f777.png)
8
- という構文をつけるだけでphpファイルが以下のエラーになります
7
+ 上記のエラーが出ます
9
8
 
10
- ```
11
- Internal Server Error
9
+ CORS対策として
10
+ PHPファイルに
11
+ ・header('Access-Control-Allow-Origin: http://localhost:3000/');
12
+ ・ header('Access-Control-Allow-Methods: GET, POST, PUT');
12
- The server encountered an internal error or misconfiguration and was unable to complete your request.
13
+ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
14
+ と記述いたしました。
15
+ ですが未解決です。
13
16
 
14
- Please contact the server administrator at postmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
17
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-09-21/9d0cb833-96bf-408c-9d87-c6c81c90d3bf.png)
15
18
 
19
+ また上記の状態(Networkタグ)の意味が分かっていません
16
- More information about this error may be available in the server error log.
20
+ プリフライトリクエストが成功したが本番のリクエストではじかれたということでしょうか
17
21
 
18
- Apache/2.4.56 (Win64) OpenSSL/1.1.1t PHP/8.2.4 Server at localhost Port 80
19
- ```
22
+ よろしくお願いいたします。
20
23
 
21
24
  ### 該当のソースコード
22
25
 
23
26
  ```PHP
27
+
24
28
  <?php
25
- header("Access=Control-Allow-Origin: http://localhost:3000");
29
+ header('Access-Control-Allow-Origin: http://localhost:3000/');
30
+ header('Access-Control-Allow-Methods: GET, POST, PUT');
31
+ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
32
+
26
33
  if($_SERVER['REQUEST_METHOD'] === 'POST') {
27
34
  $raw = file_get_contents('php://input');
28
- file_put_contents('../../../players/players_names/src/components/players_list.json', json_encode($raw));
35
+ file_put_contents('../../../players/players_names/src/components/players_list.json', json_encode($raw)); //JSONの相対パス
29
36
  }
37
+
30
38
  ?>
31
39
 
32
40
  ```
33
41
 
34
- ### 試したこと
42
+ ```JavaScript
43
+ async function save(players) {
35
44
 
36
- header("Access=Control-Allow-Origin: http://localhost:3000"); を取り除くとうまく作動します
45
+ const headers = {'Content-Type': 'application/json'};
37
- ですがうまくバックエンドとして機能させたいと考えております。
38
- よろしくお願いいたします。
39
46
 
47
+ players = reobj;
48
+ await axios.post("http://localhost:80/players_backend/index.php",
49
+ players, //playersという値をphpに渡したい
50
+ {heders:headers}
51
+ ).then((res) => {
52
+ console.log(res);
53
+ })
54
+ .catch((error) => {
55
+ console.log(error);
56
+ });
57
+ }
58
+ save();
59
+ ```
60
+
61
+