Pusher Ajax呼叫似乎不起作用

时间:2014-09-15 20:38:40

标签: c# javascript ajax backbone.js pusher

第一次尝试使用Pusher并且......不确定它是否正常工作。

当页面加载时,console.log代码中的js语句会触发,表明Pusher对象经历了以下状态:Initialized > Connecting > Connected < / p>

但是,我已经对connected事件进行了简单的ajax调用,希望只发送Pusher.connection.socked_id,然后接收success。进入ajax调用的socket_id函数后,我只想显示返回的Successful Ajax Call

同样,我的所有显示都显示在控制台中,使应用看起来正常,包括success回调中的data消息,但我没有收到任何数据。 undefined对象显示为HandleEvent,如果我在 [HttpPost] public void HandleEvent(string socket_id) { var pusher = new Pusher(PusherConfig.APP_ID(), PusherConfig.KEY(), PusherConfig.SECRET()); var result = pusher.Trigger("test_channel", "my_event", new { message = socket_id }); } 服务器端方法中设置了断点,则它永远不会触发。

知道我做错了什么吗?

C#

$(document).ready(function () {   
Pusher.log = function(message) {
    if (window.console && window.console.log) {
       console.log(message);
    }
};

var pusher = new Pusher(key);
var socketId = null;
var channel = pusher.subscribe('test_channel');
channel.bind('my_event', function (data) {
    alert(data.message);
});
pusher.connection.bind('initialized', function (data) {
    console.log('initialized');
});
pusher.connection.bind('connecting', function (data) {
    console.log('connecting');
});
pusher.connection.bind('connected', function (data) {
    console.log('connected');
    socketId = pusher.connection.socket_id;

    $.ajax({
        url: 'api/Values/HandleEvent',
        type: "POST",
        data: { socket_id: socketId },
        success: function(data) {
            console.log("Succesful Ajax Call");
            console.log("Data: " + data);
        },
        error: function(error) {
            console.log("Bad Ajax Call");
        }
    });
});
pusher.connection.bind('unavailable', function (data) {
    console.log('unavailable');
});
pusher.connection.bind('failed', function (data) {
    console.log('failed');
});
pusher.connection.bind('disconnected', function (data) {
    console.log('disconnected');
});
console.log("State: " + pusher.connection.state);

Pusher App

{{1}}

});

1 个答案:

答案 0 :(得分:0)

由于响应在json中返回,请尝试以下命令。

查看完整回复

alert(JSON.stringify(data));

仅查看消息

alert( data.message);