0

我正在尝试用 pusher+php+jquery+ajax 制作一个实时音乐播放器。

当用户点击#play 按钮时,它会发送一个ajax post 到一个php 脚本,该php 脚本会将数据推送到特定的通道,并将一个json 编码的数据发送回给用户。
一个 jquery 将解析出 php 脚本发回的 ajax 数据。
如果它的 raction 为 "true" ,则播放歌曲,如果为 "false" 则停止歌曲。

但是我一直在为函数 P/SI 获取未定义的数据,actionCall在我的 html 页面中包含了 myjquery 脚本、推送器脚本、soundmanager2 脚本。

我的脚本如下:

jQuery:

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:

<?php

session_start();

include_once 'Pusher.php';

$pusher = new Pusher(
    'i have added my app key', //APP KEY
    'i have added my app secret', //APP SECRET
    'added my app id' //APP ID
);

$action = $_POST['action'];

$pusher->trigger(
    'presence-music', //the channel
    'new_action', //the event
    array('raction' => $action) //the data to send
);


echo json_encode(array(
    'raction' => $action,
    'success' => true
));
exit();
?>

PHP 脚本有效,返回:

{"raction":"true","success":true}

问题是这个错误(在jquery中):

data is undefined
if(data.raction == 'true'){
4

2 回答 2

0

actionCall(msg); 

代替

actionCall(msg.data); 

因为它是包含您的结果的 msg 对象

于 2012-04-09T11:50:09.570 回答
0

您将一个未定义的变量传递给您的actionCall.

传递给您的 ajax 请求的成功回调的msg变量是服务器发送给您的。这清楚地记录在这里。因为dataType设置为 'json' jQuery 会为您解析结果并将对象传递给回调函数。

因此,出于所有意图和目的,如果 ajax 请求成功,它的成功回调如下所示:

function(msg = {"raction":"true","success":true}) {
    if (msg.success) {
        successCallback(msg);
    } else {
        alert(msg.errormsg);
    }
}

(实际上不工作的javascript,只是为了解释的目的)

然后将msg变量传递给 click 事件中定义的回调,如下所示:

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);
}

(实际上不工作的javascript,只是为了解释的目的)

然后你调用actionCall试图传递的函数,msg.data但你现在可能推断出data实际上并没有在msg对象中定义。

尝试将msg其作为一个整体传递,它应该可以工作。

所以为 ajax 调用定义的回调看起来像这样:

function(msg) {
    player.php
    actionCall(msg);
}
于 2012-04-09T12:29:39.023 回答