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

我目前正在开发一个聊天项目..我正在使用一个PHP框架,并设法在节点上运行,现在我遇到的问题是,Ajax查询不起作用,它不会发送一个单一的数据给我数据库..我使用的脚本是完美的工作,因为我使用这个脚本,当我仍然使用一个聊天应用程序的ajax长轮询…现在只是没有工作,当我在新的聊天应用程序使用节点我正在开发…这是我的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 MYH: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); }); }); 

这些是我迄今为止所拥有的。 由于某种原因,它不会'存储到我的数据库我不知道我在这里做错了什么或错过添加的东西。

尝试在node.js中直接与mysql通话。 也很好创build一个新的用户名,而不是作为根login到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()