質問編集履歴

11

表示内容変更

2018/03/26 22:36

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  >是非、返信お願いします。
40
40
 
41
- ```node.ja
41
+ ```node.js
42
42
 
43
43
  // nodeのコアモジュールのhttpを使う
44
44
 

10

表記内容

2018/03/26 22:36

投稿

Nitta
Nitta

スコア96

test CHANGED
@@ -1 +1 @@
1
- ajax通信をnodeで利用したい!
1
+ ajax通信をnodeでやることはできますか?
test CHANGED
File without changes

9

表記内容

2018/03/26 06:26

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,13 @@
18
18
 
19
19
 
20
20
 
21
+ 試した事:
22
+
23
+ ①mysqlからデータは取得可能でテンプレートにもデータ格納可能。
24
+
25
+ (ejsにデータ表示され、selectタブで選択可能を確認)
26
+
21
- 試した事:jqueryのイベントではalertが上がる。
27
+ jqueryのイベントではalertが上がる。
22
28
 
23
29
  (ajax~failを停止して確認済み)
24
30
 
@@ -52,7 +58,7 @@
52
58
 
53
59
 
54
60
 
55
- //mysqlに接続
61
+ //mysqlに接続(自作モジュール)
56
62
 
57
63
  var connection = require('./mysqlConnection');
58
64
 

8

表記内容の変更

2018/03/26 06:25

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,13 @@
18
18
 
19
19
 
20
20
 
21
+ 試した事:jqueryのイベントではalertが上がる。
22
+
23
+ (ajax~failを停止して確認済み)
24
+
25
+
26
+
21
- >ajaxから送信する際のイベントの実施方法が
27
+ >ajaxから送信する際のイベントの設定方法が
22
28
 
23
29
  >悪いのでしょうか?それとも受信側のコードが悪いのでしょうか?
24
30
 

7

表記内容

2018/03/26 06:14

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  >それをデータ送信するようにしています。
16
16
 
17
+ >(mysqlからデータ取得し、selectタグに挿入は可能)
18
+
17
19
 
18
20
 
19
21
  >ajaxから送信する際のイベントの実施方法が

6

表記内容変更

2018/03/26 05:35

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -1,11 +1,21 @@
1
1
  >はじめまして、node初心者のものです。
2
2
 
3
+ >mysql+nodeでやっています。
4
+
3
5
  >今、ajax通信でデータ送信したものを
4
6
 
5
7
  >nodeに送りそのデータでコンソールを
6
8
 
7
9
  >上げたいのですが、上がりません。
8
10
 
11
+
12
+
13
+ >ajaxから送るデータはdbから取得したもので
14
+
15
+ >それをデータ送信するようにしています。
16
+
17
+
18
+
9
19
  >ajaxから送信する際のイベントの実施方法が
10
20
 
11
21
  >悪いのでしょうか?それとも受信側のコードが悪いのでしょうか?
@@ -142,9 +152,7 @@
142
152
 
143
153
 
144
154
 
145
- //↑↑↑初期状態でselectタグにmysqlからのデータを格納し、
155
+ //↑↑↑初期状態でselectタグにmysqlからのデータを格納
146
-
147
- //ここまではデータ格納でき、問題ない
148
156
 
149
157
 
150
158
 

5

表記内容

2018/03/26 05:33

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  >上げたいのですが、上がりません。
8
8
 
9
- >ajaxから送信する際のイベント実施の仕方が
9
+ >ajaxから送信する際のイベント実施方
10
10
 
11
11
  >悪いのでしょうか?それとも受信側のコードが悪いのでしょうか?
12
12
 

4

表記内容変更

2018/03/26 05:13

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,11 @@
6
6
 
7
7
  >上げたいのですが、上がりません。
8
8
 
9
+ >ajaxから送信する際のイベントを実施の仕方が
10
+
11
+ >悪いのでしょうか?それとも受信側のコードが悪いのでしょうか?
12
+
9
- >コードは以になります
13
+ >わかる方いらっしゃいましたら、ご教授さい
10
14
 
11
15
  >是非、返信お願いします。
12
16
 
@@ -144,8 +148,6 @@
144
148
 
145
149
 
146
150
 
147
-
148
-
149
151
  app.use(function(req, res, next){
150
152
 
151
153
  res.header("Access-Control-Allow-Origin","*");
@@ -178,35 +180,13 @@
178
180
 
179
181
  ```html
180
182
 
181
- <html>
182
-
183
- <head ng-app = "myApp">
184
-
185
- <link href="C:\Program Files\webserver\Test2\Views\reset2.css" rel="stylesheet" type="text/css">
186
-
187
- <!--↓AngularJS -->
188
-
189
- <script src="//code.angularjs.org/1.5.7/angular.min.js"></script>
190
-
191
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script>
192
-
193
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.js"></script>
194
-
195
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
196
-
197
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
198
-
199
-
200
-
201
183
  <script>
202
184
 
203
185
  //selectで選択
204
186
 
205
187
  $(function() {
206
188
 
207
-
208
-
209
- $('#selectCst').change(function() {
189
+ $('#selectCst').change(function() {
210
190
 
211
191
 
212
192
 
@@ -242,20 +222,16 @@
242
222
 
243
223
  });
244
224
 
245
-
246
-
247
-
248
-
249
225
  </script>
250
226
 
251
-
252
-
253
- <title>customer</title>
227
+ <title>customer</title>
254
228
 
255
229
  </head>
256
230
 
257
231
  <body>
258
232
 
233
+
234
+
259
235
  Customer_Master
260
236
 
261
237
  <div id="customerDisplay"></div>
@@ -284,8 +260,4 @@
284
260
 
285
261
 
286
262
 
287
- </body>
288
-
289
- </html>
290
-
291
263
  ```

3

表記方法変更

2018/03/26 04:58

投稿

Nitta
Nitta

スコア96

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  >是非、返信お願いします。
12
12
 
13
- '''node.ja
13
+ ```node.ja
14
14
 
15
15
  // nodeのコアモジュールのhttpを使う
16
16
 
@@ -156,7 +156,7 @@
156
156
 
157
157
  })
158
158
 
159
- //↓ajaxからのデータを受信できない。console上がらない。
159
+ //↓ajaxからのデータを受信できない。console上がらない。
160
160
 
161
161
  app.post('/',function(req, res){
162
162
 
@@ -172,11 +172,11 @@
172
172
 
173
173
  app.listen(8124, '127.0.0.1');
174
174
 
175
- '''
175
+ ```
176
176
 
177
177
  >表示側
178
178
 
179
- '''html
179
+ ```html
180
180
 
181
181
  <html>
182
182
 
@@ -288,4 +288,4 @@
288
288
 
289
289
  </html>
290
290
 
291
- '''
291
+ ```

2

表記内容を変更

2018/03/26 04:06

投稿

Nitta
Nitta

スコア96

test CHANGED
@@ -1 +1 @@
1
- ajax通信を利用してnodeでデータを送受信する。
1
+ ajax通信をnodeで利用したい!
test CHANGED
@@ -1,9 +1,291 @@
1
- >node初心者のものです。
2
-
3
- >ajaxでデータ送信したものをもとに
4
-
5
- >mysqlからデータを取得し、表示させる方法はありますでしょうか?
6
-
7
- >ご存じ方おられましたら返信お願いし
8
-
9
- >
1
+ >はじめまして、node初心者のものです。
2
+
3
+ >今、ajax通信でデータ送信したものを
4
+
5
+ >nodeに送りそのデータでコンソール
6
+
7
+ >上げたいですが上がりせん
8
+
9
+ >コードは以下になります。
10
+
11
+ >是非、返信お願いします。
12
+
13
+ '''node.ja
14
+
15
+ // nodeのコアモジュールのhttpを使う
16
+
17
+ var http = require('http');
18
+
19
+ var ejs = require('ejs');
20
+
21
+ var fs = require('fs');
22
+
23
+ var server = http.createServer();
24
+
25
+ const bodyParser = require('body-parser');
26
+
27
+ var express = require('express');
28
+
29
+ const app = express();
30
+
31
+
32
+
33
+ //mysqlに接続
34
+
35
+ var connection = require('./mysqlConnection');
36
+
37
+
38
+
39
+ var nitta = [];
40
+
41
+
42
+
43
+ my_sql = "";
44
+
45
+ my_sql = "select CST_CD, CST_NAME from users ;"
46
+
47
+
48
+
49
+ //接続します
50
+
51
+ connection.connect();
52
+
53
+
54
+
55
+ var query = connection.query(my_sql);
56
+
57
+
58
+
59
+ //イベント発生
60
+
61
+ query
62
+
63
+ //エラー用
64
+
65
+ .on('error', function(err) {
66
+
67
+ console.log('err is: ', err );
68
+
69
+ })
70
+
71
+ //結果用
72
+
73
+ .on('result', function(rows) {
74
+
75
+ console.log(rows);
76
+
77
+ nitta.push(rows);
78
+
79
+ })
80
+
81
+ //終了
82
+
83
+ .on('end', function() {
84
+
85
+ console.log('end');
86
+
87
+ connection.destroy(); //終了
88
+
89
+ });
90
+
91
+
92
+
93
+
94
+
95
+ //urlencodedとjsonは別々に初期化する
96
+
97
+ app.use(bodyParser.urlencoded({
98
+
99
+ extended: true
100
+
101
+ }));
102
+
103
+
104
+
105
+ app.use(bodyParser.json());
106
+
107
+
108
+
109
+ //ejsを使用するための設定
110
+
111
+ app.set('views', __dirname + "\Views\");
112
+
113
+ app.set('view engine', 'ejs');
114
+
115
+
116
+
117
+ app.get('/', function(req, res) {
118
+
119
+ //ejs テンプレートエンジンでレンダリング
120
+
121
+ res.render('index.ejs', {
122
+
123
+ message : nitta
124
+
125
+ });
126
+
127
+ });
128
+
129
+
130
+
131
+ //2018.03.13追記
132
+
133
+ app.get('/', function (req, res) {
134
+
135
+ res.sendFile(__dirname + '\Views\reset.css');
136
+
137
+ });
138
+
139
+
140
+
141
+ //↑↑↑初期状態でselectタグにmysqlからのデータを格納し、
142
+
143
+ //ここまではデータ格納でき、問題ない
144
+
145
+
146
+
147
+
148
+
149
+ app.use(function(req, res, next){
150
+
151
+ res.header("Access-Control-Allow-Origin","*");
152
+
153
+ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
154
+
155
+ next();
156
+
157
+ })
158
+
159
+ //↓ajaxからのデータを受信しできない。console上がらない。
160
+
161
+ app.post('/',function(req, res){
162
+
163
+ var obj = {};
164
+
165
+ console.log('body: ' + JSON.stringify(req.body.name));
166
+
167
+ var rejson = JSON.stringify(req.body);
168
+
169
+ res.send(rejson);
170
+
171
+ });
172
+
173
+ app.listen(8124, '127.0.0.1');
174
+
175
+ '''
176
+
177
+ >表示側
178
+
179
+ '''html
180
+
181
+ <html>
182
+
183
+ <head ng-app = "myApp">
184
+
185
+ <link href="C:\Program Files\webserver\Test2\Views\reset2.css" rel="stylesheet" type="text/css">
186
+
187
+ <!--↓AngularJS -->
188
+
189
+ <script src="//code.angularjs.org/1.5.7/angular.min.js"></script>
190
+
191
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script>
192
+
193
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.js"></script>
194
+
195
+ <script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
196
+
197
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
198
+
199
+
200
+
201
+ <script>
202
+
203
+ //selectで選択
204
+
205
+ $(function() {
206
+
207
+
208
+
209
+ $('#selectCst').change(function() {
210
+
211
+
212
+
213
+ var str = $('#selectCst option:selected').text();
214
+
215
+ alert(str);
216
+
217
+ $.ajax({
218
+
219
+   async: false,
220
+
221
+   url: 'http://localhost:8124',
222
+
223
+   type: 'post',
224
+
225
+ //↓挿入したいデータ(mysqlでのカラム:CST_NAME)
226
+
227
+   data:{"name": str },
228
+
229
+   dataType: 'json'
230
+
231
+ }).done(function(res){
232
+
233
+   alert('OK!');
234
+
235
+ }).fail(function(xhr, status, error){
236
+
237
+   alert('Error!');
238
+
239
+ });
240
+
241
+ });
242
+
243
+ });
244
+
245
+
246
+
247
+
248
+
249
+ </script>
250
+
251
+
252
+
253
+ <title>customer</title>
254
+
255
+ </head>
256
+
257
+ <body>
258
+
259
+ Customer_Master
260
+
261
+ <div id="customerDisplay"></div>
262
+
263
+ <div id="sqlDisplay"></div>
264
+
265
+
266
+
267
+ <form id="form1" action="/" method="post">
268
+
269
+ //selectタグにmysqlからのデータ差し込み
270
+
271
+ <select id="selectCst">
272
+
273
+ <option value= "0" >Please select!!</option>
274
+
275
+ <% message.forEach(function(messageItem) { %>
276
+
277
+ <option value = "<%= messageItem.CST_CD %>"><%= messageItem.CST_NAME %></option>
278
+
279
+ <% }); %>
280
+
281
+ </select>
282
+
283
+ </form>
284
+
285
+
286
+
287
+ </body>
288
+
289
+ </html>
290
+
291
+ '''

1

質問内容の表示方法を大幅に変更

2018/03/26 03:37

投稿

Nitta
Nitta

スコア96

test CHANGED
@@ -1 +1 @@
1
- DBからのデータ送信を受信し表示ししたい
1
+ ajax通信を利用してnodeでデータ送受信する。
test CHANGED
@@ -1,327 +1,9 @@
1
- >大変失礼しました。node初心者のものです。
1
+ >node初心者のものです。
2
2
 
3
- >今、顧客マス作成し、
3
+ >ajaxでデー送信したものもとに
4
4
 
5
- >最終的に業務システムを(仕事)作成予定です。
5
+ >mysqlからデータを取得し、表示させる方法ありますしょうか?
6
6
 
7
- >今、混乱してるがmysqlかデータ取得し、
7
+ >ご存じ方おれまたら返信お願いします。
8
8
 
9
- >格納しselectタグで(ここまではOK)
10
-
11
- >選択時、ajaxでデータをpost送信し
12
-
13
- >それをsql文で検索し
14
-
15
- >また返して表示としたいのですが、うまくいきません。
16
-
17
- >なんとかしないとやばいので返信の程、宜しくお願いします。
18
-
19
-
20
-
21
- ```javascript,mysql,node
22
-
23
- var ejs = require('ejs');
24
-
25
- var path = require('path');
26
-
27
- const bodyParser = require('body-parser');
28
-
29
- var express = require('express');
30
-
31
- const app = express();
32
-
33
-
34
-
35
- //mysqlに接続
36
-
37
- var connection = require('./mysqlConnection');
38
-
39
-
40
-
41
- var nitta = [];
42
-
43
-
44
-
45
- my_sql = ""
46
-
47
- my_sql = "select CST_CD, CST_NAME from users ;"
48
-
49
-
50
-
51
- //接続します
52
-
53
- connection.connect();
54
-
55
-
56
-
57
- var query = connection.query(my_sql);
58
-
59
-
60
-
61
- //イベント発生
62
-
63
- query
64
-
65
- //エラー用
66
-
67
- .on('error', function(err) {
68
-
69
- console.log('err is: ', err );
70
-
71
- })
72
-
73
- //結果用
74
-
75
- .on('result', function(rows) {
76
-
77
- console.log(rows);
78
-
79
-
80
-
81
- nitta.push(rows);
82
-
83
- })
84
-
85
- //終了
86
-
87
- .on('end', function() {
88
-
89
- console.log('end');
90
-
91
- connection.destroy(); //終了
92
-
93
- });
94
-
95
-
96
-
97
- //urlencodedとjsonは別々に初期化する
98
-
99
- app.use(bodyParser.urlencoded({
100
-
101
- extended: true
102
-
103
- }));
9
+ >
104
-
105
-
106
-
107
- app.use(bodyParser.json());
108
-
109
-
110
-
111
- // ejsを使用するための設定
112
-
113
- app.set('views', __dirname + "\Views\");
114
-
115
- app.set('view engine', 'ejs');
116
-
117
-
118
-
119
- app.get('/', function(req, res) {
120
-
121
- //ejs テンプレートエンジンでレンダリング
122
-
123
- res.render('index.ejs', {
124
-
125
- message : nitta
126
-
127
- });
128
-
129
- });
130
-
131
-
132
-
133
- //2018.03.13追記
134
-
135
- app.get('/', function (req, res) {
136
-
137
- res.sendFile(__dirname + '\Views\reset.css');
138
-
139
- });
140
-
141
-
142
-
143
- app.use(function(req, res, next){
144
-
145
- res.header("Access-Control-Allow-Origin","*");
146
-
147
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
148
-
149
- next();
150
-
151
- })
152
-
153
-
154
-
155
- //postデータ受信
156
-
157
- app.post('/',function(req, res){
158
-
159
- var obj = {};
160
-
161
- console.log('body: ' + JSON.stringify(req.body.name));
162
-
163
- var rejson = JSON.stringify(req.body);
164
-
165
- res.send(rejson);
166
-
167
- //ここへmysql接続処理?
168
-
169
- });
170
-
171
-
172
-
173
- app.listen(8124, '127.0.0.1');
174
-
175
- ```
176
-
177
- ejs側
178
-
179
- ```
180
-
181
- <html>
182
-
183
- <head ng-app = "myApp">
184
-
185
- <link href="C:\Program Files\webserver\Test2\Views\reset2.css" rel="stylesheet" type="text/css">
186
-
187
- <!--↓AngularJS -->
188
-
189
- <script src="//code.angularjs.org/1.5.7/angular.min.js"></script>
190
-
191
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script>
192
-
193
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.js"></script>
194
-
195
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
196
-
197
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
198
-
199
-
200
-
201
- <script>
202
-
203
-
204
-
205
- $(function() {
206
-
207
-
208
-
209
- $('#selectCst').change(function() {
210
-
211
-
212
-
213
- var str = $('#selectCst option:selected').text();
214
-
215
-
216
-
217
- alert(str);
218
-
219
-
220
-
221
- $.ajax({
222
-
223
- async: false,
224
-
225
- url: 'http://localhost:8124',
226
-
227
- type: 'post',
228
-
229
- //↓挿入したいデータ(mysqlでのカラム:CST_NAME)
230
-
231
- data:{"name": str },
232
-
233
- dataType: 'json'
234
-
235
- }).done(function(res){
236
-
237
-
238
-
239
- console.debug(res);
240
-
241
-
242
-
243
- }).fail(function(xhr, status, error){
244
-
245
- alert(status);
246
-
247
- });
248
-
249
-
250
-
251
- });
252
-
253
-
254
-
255
- });
256
-
257
-
258
-
259
- </script>
260
-
261
-
262
-
263
- <title>customer</title>
264
-
265
- </head>
266
-
267
- <body>
268
-
269
- Customer_Master
270
-
271
- <div id="customerDisplay"></div>
272
-
273
- <div id="sqlDisplay"></div>
274
-
275
-
276
-
277
- <form id="form1" action="/" method="post">
278
-
279
- <select id="selectCst">
280
-
281
- <option value= "0" >Please select!!</option>
282
-
283
- <% message.forEach(function(messageItem) { %>
284
-
285
- <option value = "<%= messageItem.CST_CD %>"><%= messageItem.CST_NAME %></option>
286
-
287
- <% }); %>
288
-
289
- </select>
290
-
291
- <p>
292
-
293
- <input type="button" name="btnInsert" id="btnInsert" value="add">
294
-
295
- <input type="button" name="btnUpdate" id="btnUpdate" value="update">
296
-
297
- <input type="button" name="btnDelete" id="btnDelete" value="delete">
298
-
299
- //↓↓mysqlからの返信データをこちらに差し込みたい
300
-
301
- </p>
302
-
303
- CST_CD:<input type="text" name= "txtCstCd" id="txtCstCd">
304
-
305
- <a href="" name="linkMaxCd" id="linkMaxCd">Max</a> <br />
306
-
307
- CST_NAME:<input type="text" name= "txtCstName" id="txtCstName"><br />
308
-
309
- TOWN_CD:<input type="text" name= "txtTownCd" id="txtTownCd"><br />
310
-
311
- ADDRESS:<input type="text" name= "txtAddress" id="txtAddress"><br />
312
-
313
- TEL:<input type="text" name= "txtTel" id="txtTel"><br />
314
-
315
- BIRTH:<input type="text" name= "txtBirth" id="txtBirth"><br />
316
-
317
- SEX_CD:<input type="text" name= "txtSexCd" id="txtSexCd">
318
-
319
- </form>
320
-
321
-
322
-
323
- </body>
324
-
325
- </html>
326
-
327
- ```**__~~### ボールドテキスト~~__**