Is it possible to send an object from client javascript to server code via ASP.NET?

Is it possible to send an object from client javascript to server code via ASP.NET?

+6
source share
5 answers

In ASP.NET WebForms, I would use ScriptService:

Check out these samples: http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

The GenerateScriptType attribute can be used if you want to send / receive hole objects in a service: ASP.NET ScriptService deserialization problem with derived types

 [WebService(Namespace = "http://msdnmagazine.com/ws")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [GenerateScriptType(typeof(Object1))] [GenerateScriptType(typeof(Object2))] [ScriptService] public class StockQuoteService : WebService { static Random _rand = new Random(Environment.TickCount); [WebMethod] public int GetStockQuote(string symbol) { return _rand.Next(0, 120); } } 
+4
source

Yes. One way could be to use a web method; eg:

  • Create service
  • A call from a JavaScript method, for example: DataService.Push(yourObject) ;

For instance:

Javascript methods:

 function btnGenerate_onclick(result) { DataService.Push(getDataFromSomeDiv(), onGenerateReportComplete /*callback method*/); //or //DataService.Push(document.getElementById("myDiv").innerHTML, onGenerateReportComplete /*callback method*/); } function onGenerateReportComplete(result) { alert("Success:" + result); } 

Service Methods:

 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class DataService : System.Web.Services.WebService { [WebMethod(EnableSession = true)] //If you want? public bool Push(object someObject) { //var v = someObject as MyObjectClass;//Process object return true; } } 

EDIT: How does javascript know what a DataService server is?

This will require a web service link in the markup. For example, for example:

 <asp:ScriptManager ID="sm" runat="server"> <Services> <asp:ServiceReference Path="DataService.asmx" /> </Services> </asp:ScriptManager> 

Or you can use page callbacks / methods .

+3
source

Not as such. You can serialize the object into a string, send that string to ASP.NET and then convert it to an object again on the other side.

JSON is a good serialization format for this, and you can drop simple objects directly into the various libraries that are for it (and these are listed in the penultimate section of the JSON homepage ).

For more complex objects, you will need to extract the allowed bits of data that you need in order to recreate them before doing this.

+2
source

Yes. You can use Json and do POST. If you use jQuery, you can use $ .ajax to publish the values ​​on the server side. Hope this helps.

+1
source

Ben Dotnet is right about using ScriptService in asp.net WebForms. In addition to using the ScriptService decorator, the GenerateScriptType decorator is important in order to include the complex type that you want to use. I found articles that were related to Ben, in addition to this: http://www.webreference.com/programming/asp-net-ajax/complex-data-types/index.html

This is how I was able to do exactly what you are trying. First, I defined a custom type that I wanted to use in my code behind the file.

 namespace TestProject { public class SampleData { public int id { get; set; } public string StartDate { get; set; } public string EndDate { get; set; } public SampleData() { } } public partial class SamplePage : System.Web.UI.Page { /* The rest of the SamplePage.aspx.cs file goes here */ } } 

Then I created the WebMethod / ScriptMethod method in the SamplePage code as follows:

 [WebMethod] [ScriptMethod] [GenerateScriptType(typeof(SampleData))] public static bool EditReminder(SampleData data) { /* Server side code goes here */ return true; } 

Then, on the client page, I managed to create an object of type SampleData and pass it using PageMethods such as this. Remember to include the namespace, this is necessary.

 function some_javascript_function () { var sample_data = new TestProject.SampleData() sample_data.id = 1; sample_data.StartDate = '6/24/1976'; sample_data.EndDate = '3/20/2012'; PageMethods.EditReminder(sample_data, OnEditReminderComplete) } function OnEditReminderComplete () { if (result) alert("Success!"); else alert("Failure!"); } 

Also, be sure to enable the script manager and enable page methods such as this somewhere on your page:

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> 
+1
source

Source: https://habr.com/ru/post/888792/


All Articles