Php pusher,按下动作触发soundmanager.play();
我正在尝试用pusher+php+jquery+ajax制作一个实时音乐播放器 当用户单击play按钮时,它将向php脚本发送ajax帖子,php脚本将数据推送到特定频道,并将json编码的数据发送回用户。 jquery将解析出php脚本返回的ajax数据。 如果它是真的,那么它播放歌曲,如果是假的,它停止歌曲 但我一直在为actionCall函数获取未定义的数据 P/S我已经将myjquery脚本、pusher脚本、soundmanager2脚本包含到我的html页面中 我的脚本如下: jQuery:Php pusher,按下动作触发soundmanager.play();,php,jquery,ajax,pusher,Php,Jquery,Ajax,Pusher,我正在尝试用pusher+php+jquery+ajax制作一个实时音乐播放器 当用户单击play按钮时,它将向php脚本发送ajax帖子,php脚本将数据推送到特定频道,并将json编码的数据发送回用户。 jquery将解析出php脚本返回的ajax数据。 如果它是真的,那么它播放歌曲,如果是假的,它停止歌曲 但我一直在为actionCall函数获取未定义的数据 P/S我已经将myjquery脚本、pusher脚本、soundmanager2脚本包含到我的html页面中 我的脚本如下: jQu
function ajaxCall(ajax_url, ajax_data, successCallback) {
$.ajax({
type: "POST",
url: ajax_url,
dataType: "json",
data: ajax_data,
time: 10,
success: function(msg) {
if (msg.success) {
successCallback(msg);
} else {
alert(msg.errormsg);
}
},
error: function(msg) {}
});
}
function actionCall(data) {
if (data.raction == 'true') {
MP3.play();
}
else {
MP3.stop();
}
}
$(document).ready(function() {
pusher = new Pusher('i have added my app key');
Pusher.channel_auth_endpoint = 'pusher_auth.php';
nettuts_channel = pusher.subscribe('presence-music');
pusher.connection.bind('connected', function() {
nettuts_channel.bind('new_action', function(data) {
actionCall(data);
});
});
soundManager.url = 'swf';
soundManager.flashVersion = 8;
var MP3;
var currentSong = 0;
soundManager.onready(function() {
MP3 = soundManager.createSound({
id: 'sound' + currentSong,
url: "mp3/song.mp3"
});
});
$('#play').click(function() {
ajaxCall('player.php', {
'action': 'true'
}, function(msg) {
player.php
actionCall(msg.data);
});
});
$("#stop").click(function() {
ajaxCall('player.php', {
'action': 'false'
}, function(msg) {
player.php
actionCall(msg.data);
});
});
});
PHP:
问题在于jquery中存在以下错误:
data is undefined
if(data.raction == 'true'){
做
而不是
actionCall(msg.data);
因为它是包含结果的msg对象,所以将未定义的变量传递给actionCall 传递给ajax请求成功回调的msg变量是服务器发送给您的。这显然是错误的。由于数据类型设置为“json”,jQuery将为您解析结果并将对象传递给回调函数 因此,无论出于何种目的,如果ajax请求成功,其成功回调如下所示:
function(msg = {"raction":"true","success":true}) {
if (msg.success) {
successCallback(msg);
} else {
alert(msg.errormsg);
}
}
function(msg = {"raction":"true","success":true}) {
player.php /* This looks weird btw, I don't know what this is supposed to do but I'll ignore it since this doesn't look to be the problem. */
actionCall(msg.data);
}
function(msg) {
player.php
actionCall(msg);
}
不实际使用javascript,只是为了解释
然后将msg变量传递给click事件中定义的回调,如下所示:
function(msg = {"raction":"true","success":true}) {
if (msg.success) {
successCallback(msg);
} else {
alert(msg.errormsg);
}
}
function(msg = {"raction":"true","success":true}) {
player.php /* This looks weird btw, I don't know what this is supposed to do but I'll ignore it since this doesn't look to be the problem. */
actionCall(msg.data);
}
function(msg) {
player.php
actionCall(msg);
}
不实际使用javascript,只是为了解释
然后调用actionCall函数,试图传递msg.data,但可以推断,到目前为止,数据实际上并未在msg对象中定义
尝试将msg作为一个整体传递,它应该会起作用
因此,为ajax调用定义的回调如下所示:
function(msg = {"raction":"true","success":true}) {
if (msg.success) {
successCallback(msg);
} else {
alert(msg.errormsg);
}
}
function(msg = {"raction":"true","success":true}) {
player.php /* This looks weird btw, I don't know what this is supposed to do but I'll ignore it since this doesn't look to be the problem. */
actionCall(msg.data);
}
function(msg) {
player.php
actionCall(msg);
}
首先,您可能应该将所有js添加到一个立即自动执行的函数中,这样您就不会用函数污染全局范围。函数${/*您的js在这里*/}jQuery;