SignalR : negotiate parsing error : SyntaxError: Unexpected token < in JSON at position 0
SignalR : negotiate parsing error : SyntaxError: Unexpected token < in JSON at position 0
我正在尝试使用 React js(使用 redux)应用程序设置 signalr。集线器是一个 .Net 4.5 应用程序,需要将数据广播到客户端,这是一个反应 js 应用程序。
注意:这不是.NET Core 应用程序。
协商成功,但调用 hub 方法失败并出现以下错误:
Error: Error parsing negotiate response.
at Object.error (http://localhost:3000/static/js/5.chunk.js:29352:15)
at Object.callback [as success] (http://localhost:3000/static/js/5.chunk.js:29863:32)
at fire (http://localhost:3000/static/js/5.chunk.js:16012:33)
at Object.fireWith [as resolveWith] (http://localhost:3000/static/js/5.chunk.js:16129:13)
at done (http://localhost:3000/static/js/5.chunk.js:21517:20)
at XMLHttpRequest. (http://localhost:3000/static/js/5.chunk.js:21731:19)
一点点挖掘揭示了以下错误,
SyntaxError: Unexpected token < in JSON at position 0
at Object.parse ()
at push../node_modules/signalr/jquery.signalR.js.hubConnection.fn.init._parseResponse
(http://localhost:3000/static/js/5.chunk.js:29538:26)
at Object.callback [as success] (http://localhost:3000/static/js/5.chunk.js:29861:30)
at fire (http://localhost:3000/static/js/5.chunk.js:16012:33)
at Object.fireWith [as resolveWith] (http://localhost:3000/static/js/5.chunk.js:16129:13)
at done (http://localhost:3000/static/js/5.chunk.js:21517:20)
at XMLHttpRequest. (http://localhost:3000/static/js/5.chunk.js:21731:19)
这样的协商调用 returns 状态 200 OK 结果如下,
我的集线器 class 在 .Net 45 项目中,如下所示,
[HubName("schedule")]
public class SpamHub: Hub
{
public SpamHub()
{
SendSpamEverywhere();
}
public void SendSpamEverywhere()
{
Clients.All.Spams();
}
}
SignalR配置的ReactJs中间件如下,
declare global {
interface Window {
jQuery: any
}
}
window.jQuery = $;
require('signalr');
const startSignalRConnection = (connection: SignalR.Hub.Connection) => connection.start()
.then(() => console.info('SignalR connected'))
.fail((error) => console.error('SignalR connection error: ', error))
.catch((error: any) => console.error('SignalR connection error: ', error)); // I always end up here
const signalRMiddleware = (store: any) => (next: any) => async (action) => {
const connection = $.hubConnection();
const spamHubProxy = connection.createHubProxy('schedule');
spamHubProxy.on('spams', function (response) {
console.log('********* SPAM RECEIVED ***********', response);
});
//re-establishthe connection if the connection dropped
connection.disconnected(() => setTimeout(() => {
startSignalRConnection(connection)
}, 10000))
startSignalRConnection(connection);
}
next(action);
}
export default signalRMiddleware;
任何人都可以帮我找出这段代码有什么问题吗?
经过多次调试,终于找到了问题所在。我在创建集线器连接时指的是不正确的端口。更改端口号以指向正确的端口号解决了问题。
我正在尝试使用 React js(使用 redux)应用程序设置 signalr。集线器是一个 .Net 4.5 应用程序,需要将数据广播到客户端,这是一个反应 js 应用程序。
注意:这不是.NET Core 应用程序。
协商成功,但调用 hub 方法失败并出现以下错误:
Error: Error parsing negotiate response. at Object.error (http://localhost:3000/static/js/5.chunk.js:29352:15) at Object.callback [as success] (http://localhost:3000/static/js/5.chunk.js:29863:32) at fire (http://localhost:3000/static/js/5.chunk.js:16012:33) at Object.fireWith [as resolveWith] (http://localhost:3000/static/js/5.chunk.js:16129:13) at done (http://localhost:3000/static/js/5.chunk.js:21517:20) at XMLHttpRequest. (http://localhost:3000/static/js/5.chunk.js:21731:19)
一点点挖掘揭示了以下错误,
SyntaxError: Unexpected token < in JSON at position 0 at Object.parse () at push../node_modules/signalr/jquery.signalR.js.hubConnection.fn.init._parseResponse (http://localhost:3000/static/js/5.chunk.js:29538:26) at Object.callback [as success] (http://localhost:3000/static/js/5.chunk.js:29861:30) at fire (http://localhost:3000/static/js/5.chunk.js:16012:33) at Object.fireWith [as resolveWith] (http://localhost:3000/static/js/5.chunk.js:16129:13) at done (http://localhost:3000/static/js/5.chunk.js:21517:20) at XMLHttpRequest. (http://localhost:3000/static/js/5.chunk.js:21731:19)
这样的协商调用 returns 状态 200 OK 结果如下,
我的集线器 class 在 .Net 45 项目中,如下所示,
[HubName("schedule")]
public class SpamHub: Hub
{
public SpamHub()
{
SendSpamEverywhere();
}
public void SendSpamEverywhere()
{
Clients.All.Spams();
}
}
SignalR配置的ReactJs中间件如下,
declare global {
interface Window {
jQuery: any
}
}
window.jQuery = $;
require('signalr');
const startSignalRConnection = (connection: SignalR.Hub.Connection) => connection.start()
.then(() => console.info('SignalR connected'))
.fail((error) => console.error('SignalR connection error: ', error))
.catch((error: any) => console.error('SignalR connection error: ', error)); // I always end up here
const signalRMiddleware = (store: any) => (next: any) => async (action) => {
const connection = $.hubConnection();
const spamHubProxy = connection.createHubProxy('schedule');
spamHubProxy.on('spams', function (response) {
console.log('********* SPAM RECEIVED ***********', response);
});
//re-establishthe connection if the connection dropped
connection.disconnected(() => setTimeout(() => {
startSignalRConnection(connection)
}, 10000))
startSignalRConnection(connection);
}
next(action);
}
export default signalRMiddleware;
任何人都可以帮我找出这段代码有什么问题吗?
经过多次调试,终于找到了问题所在。我在创建集线器连接时指的是不正确的端口。更改端口号以指向正确的端口号解决了问题。