简体   繁体   中英

Sending a javascript array to code behind(c#) using ajax

I'm a bit new to C# and javascript so while my question is specific I am open to any alternatives.

I have an array of values (that I have created in a javascript function) that I want to send to my code-behind file to be used in a method. From what I've researched using ajax and stringifying the array with JSON seems like the best method.

My questions are

  1. Can I pass the array using this method?

  2. How do I capture the information on the server side(in my code-behind?)

Javascript passing the values

var jsonvalues = JSON.stringify(values);
var callback = window.location.href
$.ajax({
  url: callback
  type: "POST",
  contentType: 'application/json',
  data: jsonvalues
});

I've seen many solutions using [WebMethod] or some kind of WebService to capture the data, can I use this to do work in my code-behind file without having to return data?

Here is what I'm using on my code-behind file

[WebMethod]
public static void done(string[] ids)
{
String[] a = ids;
}

I have written a in-depth example for this using ASP.NET MVC, but it can easily be adapted for WebForms.

Send data with jquery to an MVC controller

The HTML and jQuery will look almost exactly the same, with the exception of where you call the WebMethod.

If the page you are using is called Default.aspx , and the method is called Done , then your URL for the WebMethod will be Default.aspx/Done .

<script>
       // Grab the information 
       var values = {"1,","2","3"};
       var theIds = JSON.stringify(values);

       // Make the ajax call
       $.ajax({
         type: "POST",
         url: "Default.aspx/Done", // the method we are calling
         contentType: "application/json; charset=utf-8",
         data: {ids: theIds },
         dataType: "json",
         success: function (result) {
             alert('Yay! It worked!');               
         },
         error: function (result) {
             alert('Oh no :(');
         }
     });
  </script>

Your WebMethod will still be the same.

[WebMethod]
public static void done(string[] ids)
{
   String[] a = ids;
   // Do whatever processing you want
   // However, you cannot access server controls
   // in a static web method.
}

The easiest way is to use ASP.NET MVC and data bind to a list. So for a list of strings, this would be very easy. Just make a controller action that looks like this:

[HttpPost]
public ActionResult MyAction(string[] values)
{
    ... debug and see that values gets set to your array from javascript ...
}

and then pass data: values in your $.ajax call. There's no need to stringify, jQuery will figure out what to do. For more complicated list bindings, check this out (and many other resources like it talking about fancy ways to bind to complex object lists):

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

For calling [WebMethod] methods from web pages or web services, check out this guide:

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Basically though you need the url to be ServicePage.aspx/MethodName

Put your data in a hidden field with runat=server. Post the form and fetch the data normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM