0
我试图访问ajax成功函数内的套接字io连接。将套接字io连接传递给ajax成功函数
// 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);
});
}
});
我该怎么做?如果我声明成功功能内的连接,它工作正常
@布赖恩 - 我已经更新了我的答案以匹配您的编辑。下一次,请告诉那些已经回复了有针对性评论的人,您已经编辑了您的问题,以解决他们指出的问题。否则,我们永远不知道你做了编辑。 – jfriend00
好酷。它虽然工作。 – Brian