【问题标题】:Pusher Ajax call doesn't seem to be workingPusher Ajax 调用似乎不起作用
【发布时间】:2014-09-15 20:38:40
【问题描述】:

第一次尝试使用Pusher,但不确定它是否有效。

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

但是,我已将一个简单的 ajax 调用绑定到 connected 事件,希望只发送然后接收 Pusher.connection.socked_id。一旦进入 ajax 调用的success 函数,我只想显示返回的socket_id

再次,我的所有显示都显示在控制台中,使应用程序看起来正在运行,包括 success 回调中的 Successful Ajax Call 消息,但是我没有收到任何数据。 data 对象显示为undefined,如果我在HandleEvent 服务器端方法中设置断点,它永远不会触发。

知道我做错了什么吗?

C#

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

推送应用

$(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 JavaScript 库调试日志的输出。见pusher.com/docs/debugging#pusher_logging
  • 您是否能够使用假套接字 ID(可能使用 curl)手动调用“api/Values/HandleEvent”并收到正确的响应?这将是首先要测试的东西。

标签: c# javascript ajax backbone.js pusher


【解决方案1】:

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

查看完整回复

alert(JSON.stringify(data));

仅查看消息

alert( data.message);

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2016-04-23
    • 1970-01-01
    • 2011-01-13
    • 2019-11-01
    相关资源
    最近更新 更多