Skip to content Skip to sidebar Skip to footer

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:

  1. Create an .aspx page and remove all the HTML from it. Call it GetData.aspx
  2. In the code behind of GetData.aspx.cs you can receive the POST data from the HTML5 page.
  3. Do work.

  4. Use jquery.

  5. $.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

  1. Create HTML element that calls a javascript function
  2. Inside of the javascript function, use ajax to make a POST to a ASP.NET Web Page (that uses C# as back-end)
  3. Get the returned value in the ajax "success" block.
  4. Use as you wish...

Using a jSON string return

  1. Create HTML element that calls a javascript function
  2. 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.
  3. 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"