簡體   English   中英

我無法使用 socket.io 和 node.js 從數據庫中獲取數據

[英]i can't get data from database with socket.io and node.js

我是 node.js 和 socket.io 的新手,我正在嘗試使用我的簡單項目從數據庫中獲取數據。

我的 index.html 是 socket.io 實時聊天的工作示例,帶有一個簡單的 ajax 請求

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });

     $.ajax({
      url: '/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0',
        type: 'GET',
        dataType: 'json',
        timeout: 10000,
        error: function (x,e) {
            if (x.status == 0 && e == 'timeout') {
                console.log('Timeout.');
            } else if (x.status == 404) {
                console.log('Error 404.');
            } else if (x.status == 500) {
                console.log('Interal Rrror.');
            } else if (e == 'parsererror') {
                console.log('Failed Request.');
            } else {
                console.log('Unknown error.' + x.responseText);
            }
        },
        success: function (data) {
           console.log(data);
        }
    });
    </script>
  </body>
</html>

我的ajax.php

<?php
    $data = "123aa";
    echo json_encode($data);
?>

最后,但並非最不重要的是,index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);



app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){

    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

他們工作得很好,但我不知道為什么我無法訪問我的 ajax.php 並從中獲取數據

XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957690-0".16.Request.create @ socket.io-1.2.0.js:2Request @ socket.io-1.2.0.js:216.XHR.request @ socket.io-1.2.0.js:216.XHR.doPoll @ socket.io-1.2.0.js:217.Polling.poll @ socket.io-1.2.0.js:217.Polling.doOpen @ socket.io-1.2.0.js:213.Transport.open @ socket.io-1.2.0.js:112.Socket.open @ socket.io-1.2.0.js:1Socket @ socket.io-1.2.0.js:1Socket @ socket.io-1.2.0.js:13.Manager.open.Manager.connect @ socket.io-1.2.0.js:1Manager @ socket.io-1.2.0.js:1Manager @ socket.io-1.2.0.js:1lookup @ socket.io-1.2.0.js:1(anonymous function) @ (index):24
jquery-1.11.1.js:9631 XHR finished loading: GET "http://localhost:3000/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0".jQuery.ajaxTransport.send @ jquery-1.11.1.js:9631jQuery.extend.ajax @ jquery-1.11.1.js:9176(anonymous function) @ (index):34
(index):47 Failed Request.
socket.io-1.2.0.js:2 XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957755-1&sid=kPUxnMKOvT2kdHmLAAAO".16.Request.create @ socket.io-1.2.0.js:2Request @ socket.io-1.2.0.js:216.XHR.request @ socket.io-1.2.0.js:216.XHR.doPoll @ socket.io-1.2.0.js:217.Polling.poll @ socket.io-1.2.0.js:217.Polling.onData @ socket.io-1.2.0.js:2(anonymous function) @ socket.io-1.2.0.js:28.Emitter.emit @ socket.io-1.2.0.js:116.Request.onData @ socket.io-1.2.0.js:216.Request.onLoad @ socket.io-1.2.0.js:216.Request.create.xhr.onreadystatechange @ socket.io-1.2.0.js:2
socket.io-1.2.0.js:2 XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957858-2&sid=kPUxnMKOvT2kdHmLAAAO".

1 - 我試圖從url刪除&t=1432217406964-0 ,甚至找出我應該放在這里的內容,但我發現了什么。 所以我不知道這是否正確

url: '/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0',

2 - 這是我的文件樹

- chat-example-master/  
------ index.html  
------ index.js  
------ ajax.php

3 - 我的 package.json

{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "4.10.2",
    "socket.io": "1.2.0"
  }
}

那么,我可以使用 Ajax 從我的數據庫中獲取數據,以及如何做到這一點? (另外,如果有人能解釋什么對URL &l=xxxxxxxx-0 有效,那就太好了)

PS:我使用的是 Node.js v0.12.2 和 Socket.io v1.3.6
PS2:對不起我糟糕的英語
PS3:我認為我正在努力使某些事情變得不可能或錯過一些非常愚蠢的事情

我得到它!

這個簡單的項目幫助我更多地了解了 socket.io 和 node.js 的工作原理以及我可以從誰的數據庫中獲取數據(在這種情況下,沒有 Ajax http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM