使用 ajax 将 javascript 数组发送到代码隐藏(c#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11123545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:36:19  来源:igfitidea点击:

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

c#javascriptjqueryasp.netwebforms

提问by Matt Foxx Duncan

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

我对 C# 和 javascript 有点陌生,所以虽然我的问题很具体,但我愿意接受任何替代方案。

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.

我有一个值数组(我在 javascript 函数中创建的),我想将其发送到我的代码隐藏文件以在方法中使用。从我使用 ajax 研究的内容来看,使用 JSON 对数组进行字符串化似乎是最好的方法。

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?)

  1. 我可以使用这种方法传递数组吗?

  2. 如何捕获服务器端的信息(在我的代码隐藏中?)

Javascript passing the values

Javascript 传递值

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?

我已经看到许多使用 [WebMethod] 或某种 WebService 来捕获数据的解决方案,我可以使用它在我的代码隐藏文件中工作而不必返回数据吗?

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

这是我在代码隐藏文件中使用的内容

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

采纳答案by David East

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

我已经使用 ASP.NET MVC 为此编写了一个深入的示例,但它可以很容易地适用于 WebForms。

Send data with jquery to an MVC controller

使用 jquery 将数据发送到 MVC 控制器

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

HTML 和 jQuery 看起来几乎完全一样,只是调用 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.

如果您正在使用的页面被调用Default.aspx,并且方法被调用Done,那么您的 WebMethod 的 URL 将是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 WebMethodwill still be the same.

你的WebMethod将仍然是一样的。

[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.
}

回答by Milimetric

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:

最简单的方法是使用 ASP.NET MVC 并将数据绑定到列表。因此,对于字符串列表,这将非常容易。只需制作一个如下所示的控制器动作:

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

and then pass data: valuesin your $.ajaxcall. 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):

然后传入data: values你的$.ajax电话。不需要字符串化,jQuery 会弄清楚要做什么。对于更复杂的列表绑定,请查看(以及许多其他资源,例如讨论绑定到复杂对象列表的奇特方法):

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

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:

[WebMethod]要从网页或网络服务调用方法,请查看本指南:

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

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

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

基本上虽然你需要网址 ServicePage.aspx/MethodName

回答by S?ren Lorentzen

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

使用 runat=server 将您的数据放在隐藏字段中。正常发布表单并获取数据。