简体   繁体   English

我无法使用 socket.io 和 node.js 从数据库中获取数据

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

I'm new at node.js & socket.io and I'm trying to get data from a database with my simple project.我是 node.js 和 socket.io 的新手,我正在尝试使用我的简单项目从数据库中获取数据。

My index.html is the working example by socket.io real-time chat with a simple ajax request我的 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>

My ajax.php我的ajax.php

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

And at last, but not least, the index.js最后,但并非最不重要的是,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');
});

They are working just fine, but i can't find out why i can't reach my ajax.php and get data from it他们工作得很好,但我不知道为什么我无法访问我的 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 - I've tried to remove &t=1432217406964-0 from url , or even find out what should i put here, but i find out anything. 1 - 我试图从url删除&t=1432217406964-0 ,甚至找出我应该放在这里的内容,但我发现了什么。 So i don't know if this is right所以我不知道这是否正确

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

2 - This is my file tree 2 - 这是我的文件树

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

3 - My package.json 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"
  }
}

So, can i use Ajax to get data from my database, and how to do it?那么,我可以使用 Ajax 从我的数据库中获取数据,以及如何做到这一点? (Also, if someone could explain what does work for &l=xxxxxxxx-0 in URL it would be great) (另外,如果有人能解释什么对URL &l=xxxxxxxx-0 有效,那就太好了)

PS: I'm using Node.js v0.12.2 and Socket.io v1.3.6 PS:我使用的是 Node.js v0.12.2 和 Socket.io v1.3.6
PS2: Sorry for my terrible english PS2:对不起我糟糕的英语
PS3: I think that i'm trying to make something impossible or missing something very stupid PS3:我认为我正在努力使某些事情变得不可能或错过一些非常愚蠢的事情

I get it!我得到它!

This simple project helped me to understand a little bit more about who socket.io an node.js works and who can I get data from my database (in this case, without Ajax这个简单的项目帮助我更多地了解了 socket.io 和 node.js 的工作原理以及我可以从谁的数据库中获取数据(在这种情况下,没有 Ajax http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/ http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM