[英]Pusher Ajax call doesn't seem to be working
第一次嘗試使用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);
});
由於響應以 json 形式返回,請嘗試以下命令。
查看完整回復
alert(JSON.stringify(data));
僅查看消息
alert( data.message);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.