Nodejsでボタンをクリックしたときに要素が出るようなajaxを作りたいのですが、初心者なので
うまくいきません。
http://localhost:8124/hello_ajax.js
をブラウザで起動して、更新するとhellowとconsole.logが出てくるので、
ローカルサーバの場合、サーバ上でNodejsファイルを実行する方法は、htmlをブラウザで起動するのではなく、
実行したいJSファイルをブラウザで起動するという事で良いでしょうか?
htmlにすると下記のエラーが出てしまいうまくいきません。
http://localhost:8124/hello_ajax.html:1 GET http://localhost:8124/hello_ajax.html net::ERR_CONNECTION_REFUSED
このサイトにアクセスできません
localhost で接続が拒否されました。
localhost 8124 hello ajax を Google で検索してください
ERR_CONNECTION_REFUSED
・tree
C:.
│ hello_ajax.html
│ │
└─scripts
hello_ajax.js
server.js
・
ローカルサーバ
\nodejs\node-ajax\scripts>node test.js
Server running at http://localhost:8124/
hello
・ちなみにhtmlを別タブで起動してボタンを押すとサーバーエラーが発生しました。がでてきます。これはajaxのjsファイル自体はサーバ上で実行されている物の、サーバとの通信がうまくいっていないという意味でしょうか?
xhr.onreadystatechange = function() { // 4は通信成功 if (xhr.readyState === 4) { console.log('tuushinn'); // 0以外なら通信に成功 if (xhr.status === 200) { result.textContent = xhr.responseText; } else { result.textContent = 'サーバーエラーが発生しました。'; } } else { result.textContent = '通信中...'; } };
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/19 00:29
2017/05/19 01:18
2017/05/19 01:33
2017/05/19 01:37