回答編集履歴

1

sample

2020/07/11 15:57

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -13,3 +13,113 @@
13
13
  当然同じファイルをwindow.openしても先程の通信とは別で開くので
14
14
 
15
15
  ajaxで以前送ったデータは保持されていません
16
+
17
+
18
+
19
+ # sample
20
+
21
+ - mypage.html
22
+
23
+ ```HTML
24
+
25
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
26
+
27
+ <script>
28
+
29
+ $(function(){
30
+
31
+ $('#btn').on('click',function(){
32
+
33
+ var basicinfo = {
34
+
35
+ name: "hoge",
36
+
37
+ scoreNum: 123
38
+
39
+ };
40
+
41
+ $.ajax({
42
+
43
+ url:'myapi.php',
44
+
45
+ type:'POST',
46
+
47
+ data:basicinfo,
48
+
49
+ }).done(function(data) {
50
+
51
+ var nWin=window.open('newpage.html');
52
+
53
+ nWin.addEventListener('load',function(){
54
+
55
+ nWin.document.querySelector('#view').textContent=data;
56
+
57
+ });
58
+
59
+ }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
60
+
61
+ alert("error");
62
+
63
+ });
64
+
65
+ });
66
+
67
+ });
68
+
69
+ </script>
70
+
71
+ <input type="button" value="open" id="btn">
72
+
73
+ ```
74
+
75
+ - myapi.php
76
+
77
+ ```PHP
78
+
79
+ <?php
80
+
81
+ $name=$_POST['name'];
82
+
83
+ $scoreNum=$_POST['scoreNum'];
84
+
85
+ $result = 'name:' . $name . ' scoreNum:' . $scoreNum;
86
+
87
+ echo $result;
88
+
89
+ ```
90
+
91
+ - newpage.html
92
+
93
+ ```HTML
94
+
95
+ <!doctype html>
96
+
97
+ <html lang="ja">
98
+
99
+ <head>
100
+
101
+ <!-- Required meta tags -->
102
+
103
+ <meta charset="utf-8">
104
+
105
+ <meta name="viewport">
106
+
107
+
108
+
109
+ <title>PHP</title>
110
+
111
+ <body>
112
+
113
+ <main>
114
+
115
+ <pre id="view">
116
+
117
+ </pre>
118
+
119
+ </main>
120
+
121
+ </body>
122
+
123
+ </html>
124
+
125
+ ```