【问题标题】:Sending JSON with SignalR使用 SignalR 发送 JSON
【发布时间】:2013-01-04 13:14:52
【问题描述】:

我想使用 Signalr 将对象从 Javascript 发送到服务器,但未调用服务器端方法。

// Model-Class generated by EF5.0
public partial class ttFragen
{   
     ...
    public long ID { get; set; }
    public Nullable<long> UserID { get; set; }
    public string Titel { get; set; }
    public string Text { get; set; }
    public Nullable<int> ProductID { get; set; }
    public Nullable<int> Score { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
}

public class Chat : Hub
{

    public void Send2(ttFragen frage)
    {
       ...
    }

}

客户代码:

 $.connection.hub.start().done(function() {

 $("#broadcast").click(function () {
 var frage = {
            ID: -1,
            UserID: -1,
            Titel: "titel2",
            Text:"testsst",
            ProductID: -1,
            Score: -1,
            Date : null
           };

chat.server.Send2(frage); // nothing happens
});

【问题讨论】:

    标签: c# javascript asp.net json signalr


    【解决方案1】:

    在客户端上,函数必须以小写字母开头。因此将调用更改为:

    chat.server.send2(frage);
    

    解决问题

    【讨论】:

    • 我为这个问题浪费了半天时间。似乎很奇怪,不仅强制使用大小写样式,而且在使用错误大小写时悄悄失败。
    • @SonicSoul -> 您可能希望在您的 Hub 方法上使用 [HubMethodName("")],这样您可以根据需要自定义方法名称
    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多