起初我想对我的“批判性”英语说抱歉。我希望它能够成为这个社区的一部分。 :)
我尝试使用ajax / JQuery和PHP / silex编写聊天。
这是我的JS代码:
$(document).ready(function () {
console.log('data');
setInterval(function () {
$.ajax({
url: './../resources/controller/AjaxCom.php',
type: 'POST',
method: 'POST',
data: formdata,
success: function () {
console.log(data);
},
error: function () {
console.log('error');
}
});
return false;
}, 1000);
$.('form').submit(
function () {
var daten = $('#message').val();
$.ajax({
url: './../resources/controller/AjaxCom.php',
type: 'POST',
method: 'POST',
data: {message: daten},
success: function (daten) {
console.log(daten);
},
error: function () {
console.log('error at writing');
}
});
});
});
这是PHP文件。这应该传达Ajax,但我得到这个错误:
POST http://localhost/chat/resources/controller/AjaxCom.php 500
(Internal Server Error)k.cors.a.crossDomain.send @ jquery.js:
4n.extend.ajax @ jquery.js:4(anonymous function) @ ServerCom.js:7
ServerCom.js:15 error
<?php
namespace resources\controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
/**
* Class AjaxCom
* @package resources\controller
*/
class AjaxCom implements AjaxComInterface
{
/**
* @var array
*/
private $chatOutput = [];
/**
* @var array
*/
private $userOutput = [];
/**
* @var
*/
private $ddConnect;
/**
* @return array
*/
public function getChatOutput()
{
return $this->chatOutput;
}
/**
* @return array
*/
public function getUserOutput()
{
return $this->userOutput;
}
/**
* @param Application $app
*/
public function __construct($datenbankVerbindung)
{
$this->ddConnect = $datenbankVerbindung;
}
/**
* @return array
*/
public function showChatData()
{
$posts = $this->ddConnect->fetchAll("SELECT message,writer FROM messages");
foreach ($posts as $post) {
$this->chatOutput[$post['message']] = $post['writer'];
}
return ($this->getChatOutput());
}
/**
* @return array
*/
public function showOnUser()
{
$users = $this->ddConnect->fetchAll("SELECT name FROM user WHERE status='1'");
foreach ($users as $user) {
array_push($this->userOutput, $user['name']);
}
return $this->getUserOutput();
}
/**
* @param $message
* @param $writer
*/
public function writeChatDateInDb($message,$writer){
$this->ddConnect->insert(
'messages',
array(
'message' => $message,
'writer' => $writer
)
);
}
}
你帮我找错了吗?非常感谢你的帮助
Freemindghost