SignalR,不是函数
SignalR, Not a Function
我正在尝试向我的 SQL 服务器发送一些数据。手头有一个模型。我收到一条错误消息,说我的 AddBug 不是一个函数。
Post:29 未捕获类型错误:chat.client.AddBug 不是一个函数
那么有人可以帮我调试这个错误吗?我找不到任何错误,对不起,我是 signalR 的新手,正在尝试为我的服务器 运行 做点什么。
客户端
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<meta name="viewport" content="width=device-width" />
<title>Post</title>
<script>
var chat;
$(document).ready(function () {
chat = $.connection.addItems;
console.log('Connection done');
$.connection.hub.start();
console.log('Connection secured');
});
function test() {
console.log('Clicked');
var a = {
Id: $('#txtId').val(),
Name: $('#txtName').val()
};
console.log('finish a');
chat.client.AddBug(JSON.stringify(a));
console.log('error');
};
</script>
我的中心 Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using WebAPItest2.Models;
using System.Data.Entity;
using Microsoft.AspNet.SignalR.Hubs;
namespace WebAPItest2
{
[HubName("addItems")]
public class queryHub : Hub
{
public void AddBug(Test bug)
{
try
{
using (var db = new testDBEntities())
{
var b = db.Tests.Create();
b.Id = bug.Id;
b.Name = bug.Name;
db.Tests.Add(b);
db.SaveChanges();
int id = b.Id;
Clients.Others.AddBug(b);
Clients.Caller.AddMeBug(b);
}
}
catch (Exception ex)
{
Clients.Caller.reportError("Unable to create bug.");
}
}
}
}
控制器
public ActionResult Post()
{
return View();
}
//hub code
public void AddBug(string id, string name)
{
//your code
}
//js server call
window.chat.server.addBug($('#txtId').val(), $('#txtName').val());
我正在尝试向我的 SQL 服务器发送一些数据。手头有一个模型。我收到一条错误消息,说我的 AddBug 不是一个函数。 Post:29 未捕获类型错误:chat.client.AddBug 不是一个函数 那么有人可以帮我调试这个错误吗?我找不到任何错误,对不起,我是 signalR 的新手,正在尝试为我的服务器 运行 做点什么。
客户端
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<meta name="viewport" content="width=device-width" />
<title>Post</title>
<script>
var chat;
$(document).ready(function () {
chat = $.connection.addItems;
console.log('Connection done');
$.connection.hub.start();
console.log('Connection secured');
});
function test() {
console.log('Clicked');
var a = {
Id: $('#txtId').val(),
Name: $('#txtName').val()
};
console.log('finish a');
chat.client.AddBug(JSON.stringify(a));
console.log('error');
};
</script>
我的中心 Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using WebAPItest2.Models;
using System.Data.Entity;
using Microsoft.AspNet.SignalR.Hubs;
namespace WebAPItest2
{
[HubName("addItems")]
public class queryHub : Hub
{
public void AddBug(Test bug)
{
try
{
using (var db = new testDBEntities())
{
var b = db.Tests.Create();
b.Id = bug.Id;
b.Name = bug.Name;
db.Tests.Add(b);
db.SaveChanges();
int id = b.Id;
Clients.Others.AddBug(b);
Clients.Caller.AddMeBug(b);
}
}
catch (Exception ex)
{
Clients.Caller.reportError("Unable to create bug.");
}
}
}
}
控制器
public ActionResult Post()
{
return View();
}
//hub code
public void AddBug(string id, string name)
{
//your code
}
//js server call
window.chat.server.addBug($('#txtId').val(), $('#txtName').val());