我需要一个单身人士课程的帮助。我正在创建一个wordpress插件,需要从服务器获得实时通知。为此,我使用了AJAX长轮询,我的代码看起来像这样。
这是一个用于提供AJAX请求和LOG类的php代码,它是单例并从项目中的许多不同位置调用
DECLARE @CustFeedCols AS NVARCHAR(4000)
DECLARE @CustFeedQuery AS NVARCHAR(4000)
SELECT @CustFeedCols = COALESCE(@CustFeedCols + ',','') + QUOTENAME(custfeed)
FROM (SELECT DISTINCT TOP 1000 replace(replace(replace(replace(custfeed ,' ',''),'.',''),'-',''),'–','') as custfeed FROM efp_touchCRM.dbo.efp_customertouch
WHERE custfeed <> '' AND custfeed is NOT null AND custfeed <> 'Sold Sample'
ORDER BY custfeed) AS custfeed
SELECT @CustFeedCols
SET @CustFeedQuery = N'SELECT ' + @CustFeedCols + ' FROM efp_touchCRM.dbo.efp_customertouch PIVOT(GROUPING(custfeed)) FOR custfeed IN (' + @CustFeedCols + ')) AS P'
SELECT @CustFeedQuery
EXEC sp_executesql @CustFeedQuery --This is where the error appears.
--Incorrect syntax near '('.
这是用于检索通知的javascript及其在wordpress中的管理部分的一部分。
if (isset($_GET['log']) && $_GET['log'] == 'true')
{
$response = array();
$response['msg'] = SI_log::get_instance()->get_message();
$response['type'] = 'something';
echo json_encode($response);
}
class SI_log{
private $log_messages = array();
private static $instance = NULL;
private $log_file;
public static function get_instance()
{
if (static::$instance === NULL) {
static::$instance = new static();
}
return static::$instance;
}
public function add_message( $message, $type )
{
array_push($this -> log_messages, $message);
}
public function get_message()
{
return end($this->log_messages);
}}?>
这是如何从另一个类调用单例类进行通知输入
<script type="text/javascript" charset="utf-8">
function waitForMsg(){
setTimeout(waitForMsg,5000);
document.getElementById("alerts").childNodes = new Array();
var request = new XMLHttpRequest();
request.open('GET', '<?php echo get_site_url() . '/wp-content/plugins/si/admin/c-si-log.php?log=true'?>', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var resp = request.responseText;
alert(resp);
var json = eval('('+resp+ ')');
document.getElementById("alerts").innerHTML= json['type'] +"<hr>";
if (json['type'] == 'WARNING'){
var html_element = '<div class="alert-message warning" id = "alert_warning"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
if (json['type'] == 'INFO'){
var html_element = '<div class="alert-message info" id = "alert_info"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
if (json['type'] == 'ERROR'){
var html_element = '<div class="alert-message errorr" id = "alert_error"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
document.getElementById("alerts") . innerHTML= html_element;
}else{
alert('<?php echo get_site_url() . '/wp-content/plugins/si/admin/c-si-log.php?log=true' ?>');
}
};
request.onerror = function() {
// There was a connection error of some sort
alert("request isnt good");
};
request.send();
}
window.onload = function (){
if (document.readyState != 'loading'){
waitForMsg();
} else {
document.addEventListener('DOMContentLoaded', waitForMsg);
}
}
</script>
我认为问题是SI_log类中的单例模式实现,因此不仅有该类的一个实例,而且还有更多,当我尝试检索通知时,即。当我触发某个动作时,通知不会存储在同一个对象中。我用警报(resp);在cilent页面中显示响应和响应,如下所示
SI_log::get_instance()->add_message("action triggered", 'INFO');
并且在log.php中你可以看到类型值很好,所以它不是通信问题。有人可以帮帮我吗?
注意:我必须使用Javascript,因为版本问题所以不要问我为什么我不使用JQuery
答案 0 :(得分:0)
当我们需要确保Web应用程序中的整个请求生命周期中只有一个类的单个实例时,单例模式非常有用。
所以,你不能以这种方式完成你想要达到的目标。
相反,将它用作基类/父类,并在需要时将其扩展到其他类。