【发布时间】:2016-08-26 00:38:05
【问题描述】:
我正在尝试在 ajax 成功函数中访问套接字 io 连接。
// Create socket connection
var socket = io.connect('http://localhost:81/');
// Ajax request gets auth token
$.ajax({
url: 'http://localhost:8000/foo',
xhrFields: {
withCredentials: true
},
success: function (result, textStatus, jqXHR) {
socket.on('connect', function () {
$('#rows').html(result);
socket.emit('message', result);
});
}
});
我该怎么做?如果我在成功函数中声明连接,它可以正常工作
【问题讨论】: