AJAX到PHP不会使用NODEJS将数据发送到数据库

金·奥利弗罗斯(Kim Oliveros)

我目前正在开发一个聊天项目。.我正在使用一个php框架,并设法在节点上运行它。我目前遇到的问题是ajax查询无法正常工作,它不会向我发送单个数据数据库..我使用的脚本非常有效,因为当我仍在对聊天应用程序使用ajax的长期轮询时使用了该脚本...现在,当我在使用node的新聊天应用程序中使用它时,它现在并没有起作用我正在开发中...这是我的index.php

<?php startblock('script') ?>
    <script src="/socket.io/socket.io.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){
            var data2 = { text: msg };

            $.ajax({
                url: 'localhost:3000/includes/message/store_chat.php',
                dataType: 'json',
                type: 'POST',
                data: {json:JSON.stringify(data2)},
                success: function (data2) {  }
            });
        });
    </script>

    <script>
        jQuery(function ($) {
            $(window).on("resize", function () {
                body      = $("html,body"),
                menu      = $("#side-menu").width(),
                gridW     = body.width() - (menu + 30),
                gridH     = body.height();

                $("#message-app-wrapper").css("height", gridH);
                $("#views-wrapper").css("width", gridW);
            }).resize();
        });
    </script>
<?php endblock(); ?>

这是数据库处理程序

<?php

//Send some headers to keep the user's browser from caching the response.
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header("Content-Type: text/plain; charset=utf-8");

$json2 = $_POST['json'];
$data = json_decode($json2);

$text = $data->text;

$con = new PDO("mysql:host=localhost:3000;dbname=schat", "root" , "");
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$sql2 = "INSERT INTO chat_storage(chat) VALUES(:msg)";
$stmt2 = $con->prepare($sql2);
$stmt2->bindValue( 'msg',$text, PDO::PARAM_STR);
$stmt2->execute();
?>

index.js在这里:

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

function getStdout(command, args, fn) {
    var childProcess = require('child_process').spawn(command, args);
    var output = '';
    childProcess.stdout.setEncoding('utf8');
    childProcess.stdout.on('data', function(data) {
        output += data;
    });
    childProcess.on('close', function() {
        fn(output);
    });
}

app.use('/assets', require('express').static(__dirname + '/assets'));
app.use('/temp', require('express').static(__dirname + '/temp'));
app.use('/includes/message', require('express').static(__dirname + '/includes/message'));
app.get('/', function(req, res) {
    //res.sendfile(__dirname + '/' +validator);
    res.send(validator);
});

//you should have only one io.on('connection')
io.on('connection', function(socket) {
    socket.on('chat message', function(msg){
        console.log('message: ' + msg);
        io.emit('chat message', msg);
    });
});

getStdout('php', ['index.php'], function(output) {
    validator = output;
    //start your server after you get an output
    http.listen(3000, function() {
        console.log(validator);
    });
});

这些是我到目前为止所拥有的。由于某种原因,它不会存储到我的数据库中,我不知道我在这里做错了什么还是错过了添加什么。

阿尔文·K。

尝试直接在node.js中与mysql对话。创建新的用户名(而不是以root用户身份登录到mysql)也很好。这是一个代码段,其中包含一些注释:

var mysql =  require('mysql'); // run: npm install mysql
var http = require('http');
var express = require('express');
var app = express();

var connection =  mysql.createConnection({ // setup the connection
        host : "localhost",
        user : "username",
        password: "password",
})

connection.connect(function(err) { // connect and handle errors
  if(err) {  
   // handle your errors here 
  }
}); // end .connect()

app.get('/path/:msg', function(req,res){ // process incoming message
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })

  var myMsg= req.params.msg; // obtain the incoming msg
  var strQuery = "INSERT INTO chat_storage(chat) VALUES(?)"; // your SQL string

  connection.query("use schat"); // select the db
  connection.query( strQuery, myMsg, function(err, rows){
     if(err) {
        // handle errors
     } else {
        // message received
     }
   }); end .query()
}); // end app.get()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Ajax将图像发送到数据库(php),而无需重新加载

如何使用ajax javascript PHP将大型json数据(250kb)发送到mysql数据库

如何使用Jquery和Ajax将Json数据发送到数据库中?

使用AJAX将数据发送到数据库失败

使用Jquery / ajax将数据发送到数据库

如何使用vue.js(ajax)将数据发送到数据库

如何通过ajax-POST使用jquery将多个字符串发送到数据库到php文件?

使用PHP将JSON信息发送到数据库?

如何使用PHP将时间发送到MySql数据库?

如何使用php将邮件从数据库发送到多个电子邮件列表

如何使用 PHP 将 HTML 输入字段发送到我的 MySQLi 数据库?

使用ajax将javascript数据发送到php

使用Ajax将数据从.js文件发送到.php

无法使用ajax将数据发送到php文件

使用AJAX和PHP将数据发送到mysql

使用.ajax将数据发送到php函数

将XML数据发送到MySQL数据库

将textarea数据发送到数据库的麻烦

将json数据发送到mysql数据库?

将数据从表单发送到数据库

当您通过“res.render”将数据发送到 HTML 模板时,使用 Ajax 获取 Mongodb 数据库

如何使用ajax将数据属性发送到laravel控制器(存储在数据库中)?

使用EtherCard.h库将arduino数据发送到我的数据库

如何将下拉列表中检索到的值的ID从数据库发送到php文件?

Android应用程序不会将图片发送到数据库PHP MYSQL

使用Ajax数据发送数据而不发送到PHP

无法将数据从用户表单发送到 PHP 中的数据库

Php 将表单数据发送到 Wordpress 中的特定 Mysql 数据库中

无法将数据发送到mysql数据库。这不是我的PHP代码