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