Naruto Naruto - 2 months ago 10
jQuery Question

Get data from AJAX and send them to SignalR Hub

Good day
I have the following code:

$('#ElementId').click(function () {
$.ajax({
type: "POST",
url: "URL",
data: {//DATA }
})
.done(function (result) {
FillList(result);
});
}

function FillList(result)
{
var chat = $.connection.chatHub;
chat.server.setResult(result);
}


I want to get data form AJAX call and pass them to SignalR but I just cant get it to work. AJAX does its work but when I try to send data to SignaR Hub nothing happens.
For any help thank you in advance

UPDATE

Although the above doesn't work when I call the function on it own without AJAX then it works:

$('#ElementId').click(function () {
FillList("Test");
}


UPDATE #2
This is how the function in question looks in SIgnalR

public void SetResult(string TestJSON)
{
try
{
if (!String.IsNullOrEmpty(TestJSON))
{
//TODO
}
}
catch (Exception ex)
{
throw ex
}

}

Ted Ted
Answer

I see two possible scenarios

  1. result is JSON and not string, so make sure that you are passing a string to SignalR otherwise it will not fire:

chat.server.setResult(JSON.stringify(result));

  1. If the above doesn't work, the problem is connectivity with your hub. To make sure that there is no datatype mismatch, call the server function with no parameters and see whether it's being hit:

chat.server.setResult();

and

public void SetResult()

After your UPDATE I'm convinced that you are passing JSON to your hub but it's expecting a string... So go with #1