DVLP / signalr-no-jquery

120 stars 77 forks source link

How to send data to server #11

Closed JoelAH closed 4 years ago

JoelAH commented 7 years ago

Hi, I didnt see an example on how to send data to the server. Only to receive. How do I send message to server?

barranger commented 7 years ago

Since others are not replying I'll take a shot at it:

you call invoke on the hubProxy.
in the following example assume that the exposed method is called callMe you would call invoke like so:

const connection = hubConnection('http://[address]:[port]', options);
const hubProxy = connection.createHubProxy('hubNameString');

// connect
connection.start({ jsonp: true })
.done(function(){ 
    hubProxy.invoke('callMe', { message:'payload' });
})
.fail(function(){ console.log('Could not connect'); });
JoelAH commented 7 years ago

Hi thanks for your reply. I have tried something similar before. I have just tried this and getting the same error. it says:

XMLHttpRequest cannot load localhost:56603/signalr/send?transport=longPolling[.other data here..]&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8100' is therefore not allowed access. The response had HTTP status code 500.

I get this error despite already using CORS and set enableJSONP to true on the server side. Plus I can get messages from the server, so connection is not the problem. If I navigate to the link above, I will get the following error from the server:

Value cannot be null. Parameter name: s

[ArgumentNullException: Value cannot be null. Parameter name: s] System.IO.StringReader..ctor(String s) +12392660 Microsoft.AspNet.SignalR.Json.JsonSerializerExtensions.Parse(JsonSerializer serializer, String json) +63 Microsoft.AspNet.SignalR.Hubs.HubRequestParser.Parse(String data, JsonSerializer serializer) +21 Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data) +40 Microsoft.AspNet.SignalR.<>c__DisplayClass64_1.b__5() +34 .......

gtwilliams03 commented 7 years ago

I am getting the same error with the same setup. We enabled WebSockets on our server tonight and I get TryWebSockets: true back during the negotiate call, but any POST data ends up being removed from the body (and my connection is falling back to longPolling also.

gtwilliams03 commented 7 years ago

One thing I did notice is that adding this line at line 73 of jQueryShim.js gets your hub call data sent in the querystring when you have fallen back to longpolling (I think because long polling uses a GET instead of a POST, the post body is ignored):

if (options.type === "GET" && options.data.data) options.url += `&data=${options.data.data}`

This might be a bad hack, so I'm not doing a pull request yet (and this might be part of the not using a current signalR client issue also...

harithFED commented 7 years ago

im asking here , can i know how to put the query string? ty, okay nvm i got it just .qs = "your qs"