我正在尝试将一些数据发送到我的SQL服务器。随着模型在手。我得到一个错误,说我的AddBug不是一个函数。 发布:29 Uncaught TypeError: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>
我的中心班
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();
}
答案 0 :(得分:0)
//hub code
public void AddBug(string id, string name)
{
//your code
}
//js server call
window.chat.server.addBug($('#txtId').val(), $('#txtName').val());