Pusher Ajax调用不会';似乎不起作用
本文关键字:不起作用 Ajax 调用 Pusher | 更新日期: 2023-09-27 18:21:55
第一次尝试使用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 });
}
Pusher应用程序
$(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);
});
由于响应以json形式返回,请尝试以下命令。
查看完整响应
alert(JSON.stringify(data));
只查看消息
alert( data.message);