How To Send Data From C# To Javascript/ajax Webpage
Basically, I have a need for a c# server to be able to send data back to an HTML5 webpage which has previously send data to the server. The server is retrieving data from another a
Solution 1:
If websockets are not an option then you are left with Comet.
Client-side you could do something like this :
(functionpoll(){
$.ajax({
url: "url",
success: function(data) { /*display data*/ },
complete: poll,
timeout: 30000 });
})();
Which means an ajax request will be sent every 30 seconds.
This is not as performant as websockets but it works quite well.
Solution 2:
- Create an .aspx page and remove all the HTML from it. Call it GetData.aspx
- In the code behind of GetData.aspx.cs you can receive the POST data from the HTML5 page.
Do work.
Use jquery.
- $.post("GetData.aspx",{name: value},function(json){ // put processing instructions here. });
Solution 3:
There are two ways to do this :
Using a ASP.NET web-page
- Create HTML element that calls a javascript function
- Inside of the javascript function, use ajax to make a POST to a ASP.NET Web Page (that uses C# as back-end)
- Get the returned value in the ajax "success" block.
- Use as you wish...
Using a jSON string return
- Create HTML element that calls a javascript function
- Inside of the javascript function, use ajax to make a POST to a Web Service (that uses C# as back-end), that returns a jSON string.
- Use the returned jSON data in which ever way needed.
I personally prefer jSON, as it emulates a data set, and works well with HTML and ajax.
Post a Comment for "How To Send Data From C# To Javascript/ajax Webpage"